zoukankan      html  css  js  c++  java
  • python io多路复用

                

    用for循环执行多用户访问

    SEVER
    
    import socket
    sk1 = socket.socket()
    sk1.bind(('127.0.0.1', 888))
    sk1.listen()
    import select
    ipt = [sk1, ]
    output = []
    message_dic={}
    eer=[]
    while True:
        r_list, w_list, e_list = select.select(ipt, output, eer, 1)
        print('正在监听的sk对象 %s' % len(ipt))
        print(r_list)
        # for con in w_list:
            # con.sendall(bytes('hey', encoding='utf-8'))
            # output.remove(con)
        for sk_con in r_list:
            if sk_con == sk1:
                # 表示新客户来
                con, addr = sk_con.accept()
                ipt.append(con)
                message_dic[con]=[]
            else:
                # 老客户发消息
                try:
                    data_bytes = sk_con.recv(1024)
                    # data_str = str(data_bytes, encoding='utf-8')
                    # sk.sendall(bytes(data_str + 'hao', encoding='utf-8'))
                except ConnectionResetError:
                    eer.append(sk_con)
                else:
                    # 用户正常发消息
                    data_str = str(data_bytes, encoding='utf-8')
                    message_dic[sk_con].append(data_str)
                    output.append(sk_con)
                    # ipt.remove(sk)
                    # con.sendall(bytes('hi',encoding='utf-8'))
                    # for sk in e_list:
                    #     ipt.remove(sk)
        for cont in w_list:
            rec_str=message_dic[cont][0]
            cont.sendall(bytes(rec_str+'hhhhh',encoding='utf-8'))
            del message_dic[cont][0]
            output.remove(cont)
        for sk in e_list:
            ipt.remove(sk)
    
    sk1=socket.socket()
    sk1.bind(('127.0.0.1',888))
    sk1.listen()
    
    import select
    ipt=[sk1,]
    output=[]
    while True:
        r_list, w_list, e_list = select.select(ipt, output, ipt, 1)
        print('正在监听的sk对象 %s' % len(ipt))
        print(r_list)
        for con in w_list:
            con.sendall(bytes('hey', encoding='utf-8'))
            output.remove(con)
        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:
                    output.append(sk)
                    # ipt.remove(sk)
            # con.sendall(bytes('hi',encoding='utf-8'))
    
        # for sk in e_list:
        #     ipt.remove(sk)
    
  • 相关阅读:
    浏览器为何禁止跨域(同源策略)
    viewPager
    How to remove focus without setting focus to another control?
    android ANR
    解决Ubuntu系统中文乱码显示问题
    USB 3.0规范中译本 第6章 物理层
    库&框架-----CDN网络引用总结
    18_如何排错
    17_今日回顾
    16_sql注入的原理及处理
  • 原文地址:https://www.cnblogs.com/ezway/p/6725589.html
Copyright © 2011-2022 走看看