Twisted我需要定期连接/断开客户端连接

2024-09-29 19:36:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个twisted tcp客户端,我想定期让它连接,接收n秒的数据流,然后断开连接。 断开连接后,在进程重新开始之前,将经过n秒。在

下面是我迄今为止尝试过的代码的一个非常简短的摘录。 当我运行代码时反应器.停止()发出,在睡眠结束后我得到一个扭曲的互联网当反应器.运行()在startClientConnection()中调用

我对使用twisted是个新手,我不知道我做错了什么。任何帮助都将不胜感激。在

class TCPClientFactory(ReconnectingClientFactory)
   def startedConnecting(self, connector):
       pass

   def buildProtocol(self, addr):
       self.resetDelay()
       return MsgProcessor()

   def clientConnectionLost(self, connector, reason):
       ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

   def clientConnectionFailed(self, connector, reason):
       ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)        


class mainClass(object):
    def __init__(self):
        ...

    def startClientConnection(self):
        reactor.connectTCP(host, port, TCPClientFactory())
        reactor.callLater(60, self.periodic_connect_manager)
        reactor.run()

    def periodic_connect_manager(self):
        reactor.stop()
        time.sleep(60)
        self.startClientConnection()

Tags: 代码selfconnectordeftwistedclassreasonreactor
1条回答
网友
1楼 · 发布于 2024-09-29 19:36:49

reactor.run()只能运行一次。在

from twisted.internet import task, reactor

def connect():
    do_connect()
    reactor.callLater(60, disconnect) # disconnect in a minute

task.LoopingCall(connect).start(120) # call connect() every 2 minutes
reactor.run()

相关问题 更多 >

    热门问题