首页 > 解决方案 > 如何从扭曲的协议方法(例如 ConnectionMade)修改变量

问题描述

我是 python 的新手,对 Twisted 还很陌生,如果标题不够清楚,我很抱歉,但我会尽力描述我的问题。

我想在我的应用程序中分离逻辑和网络部分,所以我有 2 个类 Controller 和 SimpleServer,我在 Controller 中有一个名为节点的变量,我想通过添加每个连接的客户端 IP 来更新它,这对于 python 专家来说可能很明显,但我无法得到它。请看一下我的代码。我评论了我卡住的那一行。

谢谢你的帮助

控制器.py

class Controller(object):
    nodes = []
    def __init__(self, port):
        ss= SimpleServer(port)

服务器.py

class MyServerProtocol(Protocol):
    def connectionMade(self): 
        #ctrl.nodeList.append(self.transport.getPeer()) #this is what I want to do

class ControllerFactory(Factory):
    def buildProtocol(self, addr):
        return MyServerProtocol()

class SimpleServer():
    def __init__(self, port):
        reactor.listenTCP(port, ControllerFactory()) #@UndefinedVariable
        print 'server listening on %d' %port
        reactor.run() #@UndefinedVariable

主程序

if __name__ == '__main__':
    port = 9123
    ctrl = Controller(port)

客户端

class myClientProtocol(Protocol):
    def dataReceived(self, data):
        print ("data received", data)

class myClientFactory(ClientFactory):
    def buildProtocol(self, addr):
        print 'Connected.'
        return myClientProtocol()

if __name__ == '__main__':

    print "CLIENT : connecting to : [ %s ] on [ %s ]\n" %(IP,PORT)
    reactor.connectTCP('127.0.0.1',9123, myClientFactory()) #@UndefinedVariable
    reactor.run() #@UndefinedVariable    

标签: pythonprotocolstwistedfactoryreactor

解决方案


感谢您的及时回答,以及宝贵的建议,正如我在帖子中所说,我是 python 新手,但准备提高自己。

我分析了你的变化,我开始明白原理了,但有一点我不明白:对 Factory 的调用。_init (self),而且这一行产生了错误,我把它们改成了ControllerFactory。init (self),最后我将其删除。我添加了一些错过的初始化,它的工作原理,千感谢 :)

这是按我希望的方式工作的最终代码。

控制器.py

class Controller(object):
    def __init__(self, port):
    # Don't set self.nodes as a class attribute or it is shared across all instances
    self.port = port
    self.nodes = [] 

    # Don't just use a local for SimpleServer, keep a reference on an attribute.  Also, pass the controller instance in.
    self.ss = SimpleServer(port, self)

服务器.py

class MyServerProtocol(Protocol):
    def connectionMade(self): 
        # Get the reference to the Controller instance.
        ctrl = self.factory.server.controller
        # Use it.
        ctrl.nodes.append(self.transport.getPeer())    

class ControllerFactory(Factory):
    # Set this class attribute so the inherited buildProtocol does the work for us, including setting the factory attribute.
    protocol = MyServerProtocol
    def __init__(self, server):
        # Save a reference to the given server as an attribute on the instance.
        self.server = server
        #ControllerFactory.__init__(self)

class SimpleServer():
    def __init__(self, port, controller):
        self.port = port
        self.controller = controller
        # Pass a reference to the SimpleServer instance to the ControllerFactory
        reactor.listenTCP(self.port, ControllerFactory(self))
        print 'server listening on %d' %port
        reactor.run() 

主程序

if __name__ == '__main__':
    port = 9123
    ctrl = Controller(port)

客户端

class myClientProtocol(Protocol):
    def dataReceived(self, data):
        print ("data received", data)

class myClientFactory(ClientFactory):
    def buildProtocol(self, addr):
        print 'Connected.'
        return myClientProtocol()

if __name__ == '__main__':

    print "CLIENT : connecting to : [ 127.0.0.1 ] on [ 9123 ]\n" 
    reactor.connectTCP('127.0.0.1',9123, myClientFactory())
    reactor.run()
  • 我现在试着考虑一下你的最后一句话。

推荐阅读