sever端
1 import socket
sk1 = socket.socket()
sk1.bind(('127.0.0.1',8001,))
sk1.listen() sk2 = socket.socket()
sk2.bind(('127.0.0.1',8002,))
sk2.listen() sk3 = socket.socket()
sk3.bind(('127.0.0.1',8003,))
sk3.listen()
inputs = [sk1,sk2,sk3]
import select
while True:
# [sk1,sk2,sk3]select内部自动监听sk1,sk2,sk3,三个对象,一旦某个句柄发生变化(accept())
# 如果有链接sk1
# r_list = [sk1,sk2]
r_list,w_list,e_list = select.select(inputs,[],[],1)
for sk in r_list:
conn,adress = sk.accept()
conn.sendall(bytes('hello',encoding='utf-8'))
conn.close()
客户端,可同时连接多个客户端
import socket
obj = socket.socket()
obj.connect(('127.0.0.1',8001,)) rec_bytes = obj.recv(1024)
rec_str = str(rec_bytes,encoding='utf-8')
print(rec_str)
obj.close()