服务端:
代码语言:javascript复制import socket
s = socket.socket();
host = socket.gethostname();
port = 10086;
s.bind((host,port))
s.listen(5)
while True:
c,addr = s.accept();
print("链接地址:",addr)
strMsg = "hello maomao"
c.sendall(strMsg.encode())
c.close()
pass
客户端:
代码语言:javascript复制import socket
s = socket.socket()
host = socket.gethostname()
port = 10086
s.connect((host,port))
str = s.recv(1024)
print (str)
s.close
在python3.7里,send发送必须是socket.send(bytes[, flags])需要转换