1.IO多路复用
服务端 import socket import select server = socket.socket() server.bind(('127.0.0.1',8089)) server.listen(5) server.setblocking(False) read_list = [server] while True: r_list,w_list,x_list = select.select(read_list,[],[])#监管server对象,一但对方有人来,则返回对象 for i in r_list: if i is server:#r_list中有server对象 也有conn对象,不同对象做不同处理 conn , addr =i.accept() read_list.append(conn) #conn也会阻塞也需要加入到r_list当中被select监管 else:#当为conn对象时 res= i.recv(1024) if len(res)==0: i.close() read_list.remove(i)#删除无用的对象 continue print(res) i.send(b'shoudao') 客户端 import socket client = socket.socket() client.connect(('127.0.0.1',8089)) while True: client.send(b'helllo') res = client.recv(1024) print(res)
标签:socket,res,模型,list,server,select,IO,conn From: https://www.cnblogs.com/junyong11/p/17167336.html