我有这个简单的Twisted Client连接到Twisted服务器并查询索引.如果你看到fn.connectionMade()在class SpellClient,它query是硬编码的.这是为了测试目的.如何将此查询从外部传递给此类?
代码 -
from twisted.internet import reactor
from twisted.internet import protocol
# a client protocol
class SpellClient(protocol.Protocol):
"""Once connected, send a message, then print the result."""
def connectionMade(self):
query = 'abased'
self.transport.write(query)
def dataReceived(self, data):
"As soon as any data is received, write it back."
print "Server said:", data
self.transport.loseConnection()
def connectionLost(self, reason):
print "connection lost"
class SpellFactory(protocol.ClientFactory):
protocol = SpellClient
def clientConnectionFailed(self, connector, reason):
print "Connection failed - goodbye!"
reactor.stop()
def …Run Code Online (Sandbox Code Playgroud) 我写了一个简单的扭曲服务器 -
from twisted.internet import reactor
from twisted.internet import protocol
from twisted.web import server, resource
from twisted.internet import reactor
class Index(resource.Resource):
isLeaf = True
def render_GET(self, request):
args = request.args
print 'Args: %s' %(repr(args))
print 'Serving on PORT: 8090'
site = server.Site(Index())
reactor.listenTCP(8090, site)
reactor.run()
Run Code Online (Sandbox Code Playgroud)
这运行得很好127.0.0.1:8090.注意这在终端(前景)中运行,当我使用nohup&在后台运行进程时ctrl+Z.服务器不响应请求.我应该怎么做才能守护这个扭曲的服务器
当使用 Twisted ReconnectingClientFactory 并且连接丢失时,我是否需要从 clientConnectionLost 方法中调用 connector.connect() 还是会自动发生?
答案似乎很明显,因为它毕竟是 ReconnectingClientFactory但 Twisted 文档在这里说了一些让我想知道的东西:
“调用 connector.connect() 可能很有用 - 这将重新连接。”
术语“可能有用”的措辞和使用导致了这个问题,因为基本客户端工厂的 api 文档也说了同样的事情。
Max 的答案是正确的,但经过进一步研究,我认为“更正者”的答案如下:
def clientConnectionLost(self, connector, reason):
# do stuff here that is unique to your own requirements, then:
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
Run Code Online (Sandbox Code Playgroud)
这允许您执行应用程序所需的专门操作,然后调用工厂代码以允许 Twisted 为您调用 retry()。