标签:自动 strong imp content inpu dal 读写 print 机制
I/O多路复用指:通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作
服务端
import socket
sk1 = socket.socket()
sk1.bind((‘127.0.0.1‘,8001))
sk1.listen()
sk2 = socket.socket()
sk2.bind((‘127.0.0.1‘,8001))
sk2.listen()
sk3 = socket.socket()
sk3.bind((‘127.0.0.1‘,8001))
sk3.listen()
inputs = [sk1,sk2,sk3, ]
import select
while True:
#[sk1,sk2,sk3, ],select 内部自动监听sk1,sk2,sk3三个对象
#如果有人连接sk1
#r_list = [sk1,sk2,sk3]
r_list, w_list, e_list = select.select(inputs,[],[],1)
for sk in r_list:
#连接每个对象
conn,address = sk.accept()
conn.sendall(bytes(‘hello‘,encoding=‘utf-8‘))
conn.close()
客户端sk1
import socket
obj = socket.socket()
obj.connect((‘127.0.0.1‘,8001))
content = str(obj.recv(1024),encoding=‘utf-8‘)
print(content)
obj.close()
客户端sk1
import socket
obj = socket.socket()
obj.connect((‘127.0.0.1‘,8002))
content = str(obj.recv(1024),encoding=‘utf-8‘)
print(content)
obj.close()
标签:自动 strong imp content inpu dal 读写 print 机制
原文地址:https://www.cnblogs.com/huangguabushihaogua/p/9446360.html