gpt4 book ai didi

python - 如何修改来自扭曲协议(protocol)方法(例如 ConnectionMade)的变量

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:44 24 4
gpt4 key购买 nike

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

我想将应用程序中的逻辑和网络部分分开,所以我有2个类Controller和SimpleServer,我在Controller中有一个名为nodes的变量,我想通过添加每个连接的客户端IP来更新它,这可能是显而易见的对于Python专家来说,但我无法理解。请看一下我的代码。我评论了我被卡住的那一行。

感谢您的帮助

Controller .py

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

server.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

最佳答案

您只需要引用正确的对象。

就像您需要对 react 器的引用一样,因此您导入了 twisted.internet.reactor 以便可以调用 run 方法。

Controller .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.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)

server.py

class MyServerProtocol(Protocol):
def connectionMade(self):
# Get the reference to the Controller instance.
ctrl = self.factory.server.controller
# Use it.
ctrl.nodeList.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
Factory.__init__(self)

class SimpleServer():
def __init__(self, port, controller):
self.controller = controller
# Pass a reference to the SimpleServer instance to the ControllerFactory
reactor.listenTCP(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 : [ %s ] on [ %s ]\n" %(IP,PORT)
reactor.connectTCP('127.0.0.1',9123, myClientFactory()) #@UndefinedVariable
reactor.run() #@UndefinedVariable

我做了我能看到的最小的一组更改来演示解决方案。我还没有测试生成的代码。希望您能看到我所做的就是获取对代码一部分所拥有的对象的引用,并将它们传递给需要它们的代码的其他部分。这通常采用将参数传递给函数(初始化程序、方法等)和设置对象属性(通常是 self 引用的任何内容)的形式。

我没有尝试提高代码的可维护性或纠正任何令人担忧的模式。此代码仍然不是理想软件的模型,但它至少应该允许您将一个值附加到您想要附加到的列表中。

我提出的其他一些建议是:

  • 使用更具描述性的名称
  • 减少间接性
  • 除了在 self 上设置属性之外,不要让 __init__ 产生副作用
  • 不要太严重地违反德墨忒尔法则
  • 不要做任何你正在做的事情(可能是*导入?),这会导致你在整个过程中撒上#@UndefinedVariable

关于python - 如何修改来自扭曲协议(protocol)方法(例如 ConnectionMade)的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59251180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com