首页 > 代码库 > python io多路复用

python io多路复用

            

用for循环执行多用户访问

SEVER

 

import socket
sk1=socket.socket()
sk1.bind((‘127.0.0.1‘,888))
sk1.listen()

import select
ipt=[sk1,]

while True:
    r_list, w_list, e_list = select.select(ipt, [], ipt, 1)
    print(‘正在监听的sk对象 %s‘ % len(ipt))
    print(r_list)
    
    for sk in r_list:
        if sk==sk1:
            # 表示新客户来了
            con,addr=sk.accept()
            ipt.append(con)
        else:
            # 老客户发消息
            try:
                data_bytes=sk.recv(1024)
                data_str=str(data_bytes,encoding=‘utf-8‘)
                sk.sendall(bytes(data_str+‘hao‘,encoding=‘utf-8‘))
            except ConnectionResetError:
                ipt.remove(sk)

 

python io多路复用