server:
import select import socket sk = socket.socket() sk.bind(('127.0.0.1',8000)) sk.setblocking(False) sk.listen() read_lst = [sk] while True: # [sk,conn] r_lst,w_lst,x_lst = select.select(read_lst,[],[]) for i in r_lst: if i is sk: conn,addr = i.accept() read_lst.append(conn) else: ret = i.recv(1024) if ret == b'': i.close() read_lst.remove(i) continue print(ret) i.send(b'goodbye!')
client:
import time import socket import threading def func(): sk = socket.socket() sk.connect(('127.0.0.1',8000)) sk.send(b'hello') time.sleep(3) print(sk.recv(1024)) sk.close() for i in range(20): threading.Thread(target=func).start()
IO多路复用的三种机制:
select :windows linux
poll;linux
eploo:linux
链接:https://www.cnblogs.com/Eva-J/articles/8324837.html
import selectors from socket import * def accept(sk,mask): conn,addr=sk.accept() sel.register(conn,selectors.EVENT_READ,read) def read(conn,mask): try: data=conn.recv(1024) if not data: print('closing',conn) sel.unregister(conn) conn.close() return conn.send(data.upper()+b'_SB') except Exception: print('closing', conn) sel.unregister(conn) conn.close() sk=socket() sk.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) sk.bind(('127.0.0.1',8088)) sk.listen(5) sk.setblocking(False) #设置socket的接口为非阻塞 sel=selectors.DefaultSelector() # 选择一个适合我的IO多路复用的机制 sel.register(sk,selectors.EVENT_READ,accept) #相当于网select的读列表里append了一个sk对象,并且绑定了一个回调函数accept # 说白了就是 如果有人请求连接sk,就调用accrpt方法 while True: events=sel.select() #检测所有的sk,conn,是否有完成wait data阶段 for sel_obj,mask in events: # [conn] callback=sel_obj.data #callback=read callback(sel_obj.fileobj,mask) #read(conn,1)