Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用select()的Python TCP服务器_Python_Python 3.x_Sockets_Select_Tcpserver - Fatal编程技术网

使用select()的Python TCP服务器

使用select()的Python TCP服务器,python,python-3.x,sockets,select,tcpserver,Python,Python 3.x,Sockets,Select,Tcpserver,这是我的第一个条目@stackoverflow;-)) 我需要一个python3tcp服务器,它将客户端的所有输入发送给其他参与者。服务器可以工作,但不幸的是消息只返回给发送客户端。一旦第二个客户机发送某个内容,它就会获取所有条目。因此,这些条目在输出队列中可用 为什么select()无法识别这些条目 Thx的支持 ` 处理输入 处理输出 处理“特殊情况” `您的问题是,当您从输出列表中读取内容时,您仅向该列表中添加一个对等项。您应该在向其消息队列写入内容时执行此操作 接收部分应为:

这是我的第一个条目@stackoverflow;-))

我需要一个python3tcp服务器,它将客户端的所有输入发送给其他参与者。服务器可以工作,但不幸的是消息只返回给发送客户端。一旦第二个客户机发送某个内容,它就会获取所有条目。因此,这些条目在输出队列中可用

为什么select()无法识别这些条目

Thx的支持

`

处理输入 处理输出 处理“特殊情况”
`

您的问题是,当您从
输出列表中读取内容时,您仅向该列表中添加一个对等项。您应该在向其消息队列写入内容时执行此操作

接收部分应为:

        ...
        data = s.recv(1024)
        if data:
            # A readable client socket has data
            print ('received "%s" from %s' % (data, s.getpeername()))
            # Add output channel for response
            #message_queues[s].put(data)
            # if s not in outputs:
            #    outputs.append(s)
            for aQueue in message_queues:
                message_queues[aQueue].put(data)
                if aQueue not in outputs:       # enqueue the msg
                    outputs.append(aQueue)      # and ask select to warn when it can be sent
                # AQueue.send ("Test".encode())
        else:
            ...
根据您自己的要求,您应该只将其他参与者的消息排队:

            for aQueue in message_queues:
                if aQueue != s:      # only send to other participants
                    ...

最后,您经常测试
输出中是否存在套接字。让我们想想,一个
集合
比一个
列表
更合适,就是这样!谢谢你,谢尔盖@马丁:不客气。如果有帮助的话,你能接受答案(复选标记)来告诉未来的读者这个问题得到了解决方案吗。
for s in writable:
    try:
        next_msg = message_queues[s].get_nowait()
    except queue.Empty:
        # No messages waiting so stop checking for writability.
        print ("output queue for", s.getpeername(), "is empty")
        outputs.remove(s)
    else:
        print ('sending "%s" to %s' % (next_msg, s.getpeername()))
        s.send(next_msg)
for s in exceptional:
    print ("handling exceptional condition for", s.getpeername())
    # Stop listening for input on the connection
    inputs.remove(s)
    if s in outputs:
        outputs.remove(s)
    s.close()

    # Remove message queue
    del message_queues[s]
        ...
        data = s.recv(1024)
        if data:
            # A readable client socket has data
            print ('received "%s" from %s' % (data, s.getpeername()))
            # Add output channel for response
            #message_queues[s].put(data)
            # if s not in outputs:
            #    outputs.append(s)
            for aQueue in message_queues:
                message_queues[aQueue].put(data)
                if aQueue not in outputs:       # enqueue the msg
                    outputs.append(aQueue)      # and ask select to warn when it can be sent
                # AQueue.send ("Test".encode())
        else:
            ...
            for aQueue in message_queues:
                if aQueue != s:      # only send to other participants
                    ...