【问题标题】:How to shutdown and start a server in Python Twisted?如何在 Python Twisted 中关闭和启动服务器?
【发布时间】:2012-01-15 11:27:24
【问题描述】:

我的客户端(基于twisted)应该在连接丢失时自动重新连接到服务器,我需要对此功能进行测试,这是我的测试方法,其中@todo 注释非常清楚预期的行为:

@defer.inlineCallbacks
def test_reconnect_on_connection_loss(self):
    client = SMPPClientFactory(self.config)
    client.reConnect = mock.Mock(wraps=client.reConnect)
    # Connect
    smpp = yield client.connect()

    # Bind
    yield smpp.bindAsTransmitter()

    # @todo: A connection loss is expected here
    #        the client is supposed to try reconnections
    #        for a while, the server then shall start
    #        again and the client will get connected.

    # Unbind & Disconnect
    yield smpp.unbindAndDisconnect()

    ##############
    # Assertions :
    # Protocol verification
    self.assertNotEqual(0, client.reConnect.call_count)

在服务器端,我试图在收到 bindAsTransmitter 请求后中止连接:

class LooseConnectionOnBindSMSC(SMSC):

    def handleBindAsTransmitter(self, reqPDU):
        self.sendSuccessResponse(reqPDU)

        # Connection is aborted here:
        self.transport.abortConnection()

连接成功中止,我的客户端开始尝试按预期重新连接,但它永远无法让我的服务器再次启动。

【问题讨论】:

  • Jean paul,是的,当我 abortConnection 服务器端时,服务器仍在监听...我需要一种方法来停止监听几秒钟,然后重新开始。

标签: python twisted trial


【解决方案1】:

您的服务器仍在运行(据任何人都可以从您问题中的代码中看出)。关闭一个与客户端的连接不会阻止服务器接受新连接。

停止监听端口监听的方法是使用port.stopListening()(注意它返回一个Deferred)。您可以使用另一个 reactor.listenTCP(或您第一次开始侦听时使用的任何 API)调用再次开始侦听端口。

【讨论】:

  • 对不起,我不明白你的意思。
猜你喜欢
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
相关资源
最近更新 更多