python socket 简单示例

2020-07-24 10:10:09 浏览数 (1)

服务端:

代码语言: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])需要转换

0 人点赞