python tcp select 多路复用

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 # 文件名:tcpserver.py
 4  
 5 import socket
 6 import time
 7 import select
 8 
 9 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
10 s.settimeout(600)
11 s.bind(('172.28.71.82',11223))
12 s.listen(5)
13 r_list = [s,]
14 num = 0
15 max_size=1024*1024
16 while True:
17     rl, wl, error = select.select(r_list,[],[],10)
18     num+=1
19     print('counts is %s'%num)
20     print("rl's length is %s"%len(rl))
21     for fd in rl:
22         if fd == s:
23             conn, addr = fd.accept()
24             print(addr," 连接上了")
25             r_list.append(conn)
26             msg = conn.recv(max_size)
27             print(msg.decode())
28             conn.sendall(('first----%s'%conn.fileno()).encode())
29         else:
30             
31             msg = fd.recv(max_size)
32             if not msg:
33                 print("断开了!!!")
34                 r_list.remove(fd)
35             else:
36                 fd.send(msg)
37                 print(msg.decode())
38                 
39 s.close()

 

posted @ 2022-10-11 17:28  一条晓鱼ovo  阅读(35)  评论(0编辑  收藏  举报