标签:while select bind ddr bin span 框架 异步 rem
基于select
server端
import select import socket sk = socket.socket() sk.bind((‘127.0.0.1‘, 8010)) sk.setblocking(False) sk.listen() read_lst = [sk] # while True: # [sk,conn] # select提供监听 r_lst, w_lst, x_lst = select.select(read_lst, [], []) # print(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‘bye!‘)
client端
import time import socket import threading def func(): sk = socket.socket() sk.connect((‘127.0.0.1‘, 8010)) sk.send(b‘hello‘) time.sleep(3) print(sk.recv(1024)) sk.close() for i in range(5): threading.Thread(target=func).start()
主要作用:定制异步框架
标签:while select bind ddr bin span 框架 异步 rem
原文地址:https://www.cnblogs.com/wt7018/p/11624449.html