篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python并发编程-IO模型-IO多路复用实现SocketServer相关的知识,希望对你有一定的参考价值。
import select
import socket
sk = socket.socket()
sk.bind((\'127.0.0.1\',8080))
sk.setblocking(False)
sk.listen()
read_lst = [sk] #select监听谁就放入list
while True: #[sk,conn]
r_lst,w_lst,x_lsx = select.select(read_lst,[],[])
# print(\'*******\',r_lst)
# print(sk)
for i in r_lst:
if i is sk:
conn,addr = i.accept()
# print(conn)
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\')
import socket
import time
import threading
def func():
sk = socket.socket()
sk.connect((\'127.0.0.1\',8080))
sk.send(b\'hello\')
time.sleep(1)
print(sk.recv(1024))
sk.close()
for i in range(20):
threading.Thread(target=func).start()