2022-08-26 17:25:45 +08:00
|
|
|
import socket
|
|
|
|
import random
|
|
|
|
|
|
|
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
host = "127.0.0.1"
|
2022-08-28 17:53:22 +08:00
|
|
|
port = 9999 + random.randint(0, 1000)
|
2022-08-26 17:25:45 +08:00
|
|
|
print("port:", port)
|
|
|
|
server.bind((host, port))
|
|
|
|
server.listen(5)
|
|
|
|
|
|
|
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
client.connect((host, port))
|
|
|
|
|
2022-09-09 10:16:15 +08:00
|
|
|
accept, addr = server.accept()
|
2022-08-26 17:25:45 +08:00
|
|
|
|
2022-08-28 06:54:13 +00:00
|
|
|
print("recv from client: %s" % str(addr))
|
2022-08-26 17:25:45 +08:00
|
|
|
|
2022-09-05 20:40:19 +08:00
|
|
|
client.send("send test from client".encode())
|
|
|
|
print("server recv:", accept.recv(1024).decode())
|
2022-08-26 17:25:45 +08:00
|
|
|
|
2022-09-05 20:40:19 +08:00
|
|
|
accept.send("send test from server".encode())
|
|
|
|
print("client recv:", client.recv(1024).decode())
|
2022-08-26 17:25:45 +08:00
|
|
|
|
|
|
|
accept.close()
|
|
|
|
client.close()
|
|
|
|
server.close()
|