Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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
Python 如何在Twisted中管理连接和客户端?_Python_Twisted - Fatal编程技术网

Python 如何在Twisted中管理连接和客户端?

Python 如何在Twisted中管理连接和客户端?,python,twisted,Python,Twisted,我开始使用Twisted框架,我编写了一个TCP服务器,通过Telnet连接到它,它工作得很好。现在我想使用PyUI或GTK之类的GUI来管理连接和连接的客户端(发送数据、切断连接等) 这是我的密码 import sys import os from twisted.internet import reactor, protocol from twisted.python import log class Server(protocol.Protocol): def

我开始使用Twisted框架,我编写了一个TCP服务器,通过Telnet连接到它,它工作得很好。现在我想使用PyUI或GTK之类的GUI来管理连接和连接的客户端(发送数据、切断连接等)

这是我的密码

import sys
import os
from twisted.internet import reactor, protocol
from twisted.python import log

    class Server(protocol.Protocol):

        def dataReceived(self, data):
            log.msg ("data received: %s"%data)
            self.transport.write("you sent: %s"%data)

        def connectionMade(self):
            self.client_host = self.transport.getPeer().host
            self.client_port = self.transport.getPeer().port
            if len(self.factory.clients) >= self.factory.clients_max:
                log.msg("Too many connections !!")
                self.transport.write("Too many connections, sorry\n")
                self.transport.loseConnection()
            else:
                self.factory.clients.append((self.client_host,self.client_port))
                log.msg("connection from %s:%s\n"%(self.client_host,str(self.client_port)))
                self.transport.write(
                        "Welcome %s:%s\n" %(self.client_host,str(self.client_port)))


        def connectionLost(self, reason):
            log.msg('Connection lost from %s:%s. Reason: %s\n' % (self.client_host,str(self.client_port),reason.getErrorMessage()))
            if (self.client_host,self.client_port) in self.factory.clients:
                self.factory.clients.remove((self.client_host,self.client_port))

    class MyFactory(protocol.ServerFactory):

        protocol = Server
        def __init__(self, clients_max=10):
            self.clients_max = clients_max
            self.clients = []          


    def main():
        """This runs the protocol on port 8000"""
        log.startLogging(sys.stdout)
        reactor.listenTCP(8000,MyFactory)
        reactor.run()


    if __name__ == '__main__':
        main()

谢谢。

如果您想编写一个既运行UI又运行网络的Python程序(进程),首先需要选择一个与UI工具包的事件循环集成的合适的Twistedreactor。看

接下来,您可以从一些简单的东西开始,比如有一个按钮,按下该按钮时,将向所有当前连接的客户端发送文本消息


另一件事:什么客户端将连接?浏览器(也)?如果是这样,您可能会考虑使用WebSocket而不是原始TCP。

“管理”是一个非常宽泛的非特定动词。你到底想做什么?哪一部分给您带来了困难?管理意味着断开客户机连接,向客户机发送命令,列出实时发送和接收数据等。您有什么问题?任何以“etc”结尾的元素列表都表明对于堆栈溢出来说,答案太长。我的建议是调整你的问题,只处理其中的一个问题,一旦你了解如何做这一个,你应该能够概括。也许只是“断开连接”?好的,我只是想知道如何断开客户端,向它们发送命令,抛出一个GUI,比如pygame。