python socket 简单示例

服务端:

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

客户端:

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])需要转换

你可能感兴趣的:(python socket 简单示例)