zoukankan      html  css  js  c++  java
  • 用select模拟一个socket server成型版2

    1.字典队列测试

    import queue
    
    msg_dic={}
    msg_dic[1]=queue.Queue()
    msg_dic[1].put('hello')
    msg_dic[1].put('bob')
    print(msg_dic)
    

     运行结果:

    C:abccdxdddOldboypython-3.5.2-embed-amd64python.exe C:/abccdxddd/Oldboy/Py_Exercise/Day10/eee.py
    {1: <queue.Queue object at 0x00801630>}
    
    Process finished with exit code 0
    

     2. 服务器端代码:

    import select,socket,queue
    server=socket.socket()
    server.bind(('localhost',9000))
    server.listen(1024)
    server.setblocking(False)
    msg_dic={} #定义一个空字典
    inputs=[server,]
    #inputs=[server,conn,conn2]
    outputs=[]
    #outputs=[r1,]
    while True:
        readable,writeable,exceptional=select.select(inputs,outputs,inputs)
        print(readable,writeable,exceptional)
        for r in readable:
            if r is server:
                conn,addr=server.accept()
                print('来了个新链接',addr)
                inputs.append(conn)
                msg_dic[conn]=queue.Queue() #初始化一个队列,后面存要返回给客户端的数据。键值key:value=conn(键):queue.Queue(值)
            else: #r is conn/conn2...
                data=r.recv(1024)
                print('收到数据',data)
                msg_dic[r].put(data) #把要发的数据存在字典里面,等下次循环的时候发。
                outputs.append(r) #放入返回的链接队列里.往output里面放什么,那么下次循环就出什么。往output里面放的是conn2,那么出的就是conn2
                #r.send(data)
    
    
        for w in writeable: #要返回给客户端的链接列表,writeable里面存放的是r (conn,conn2)
            data_to_client=msg_dic[w].get()
            w.send(data_to_client) #返回给客户端源数据。这样收和发就分开了。
            outputs.remove(w) #确保下次循环的时候writeable不返回这个已经处理完的链接了。
    
        for e in exceptional: #这个链接已经断开了,出问题了,我就不再检测它了。
            if e in outputs:
                outputs.remove(e)
            inputs.remove(e)
            del msg_dic[e]
    

     客户端代码:

    import socket
    s=socket.socket()
    s.connect(('localhost',9000))
    while True:
        msg=bytes(input(">>:"),encoding='utf8')
        s.sendall(msg)
        data=s.recv(1024)
        print('Received',data)
    s.close()
    
  • 相关阅读:
    路由控制
    NodeJS -Express 4.0 用include取代partial
    工程的结构文件
    Express 框架的安装
    iconfont阿里爸爸做的开源图库
    12.文件系统fs
    11.事件驱动events
    10.Node.js核心模块
    Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service
    Apache CXF实现Web Service(1)——不借助重量级Web容器和Spring实现一个纯的JAX-WS web service
  • 原文地址:https://www.cnblogs.com/momo8238/p/7388733.html
Copyright © 2011-2022 走看看