如何用pythontwisted关闭和启动服务器?

2024-07-06 23:31:46 发布

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

我的客户端(基于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请求后中止连接:

^{pr2}$

连接被成功中止,我的客户端开始尝试重新连接,但它再也没有办法让我的服务器重新启动。在


Tags: theself功能服务器client客户端istwisted
1条回答
网友
1楼 · 发布于 2024-07-06 23:31:46

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

阻止侦听端口侦听的方法是使用port.stopListening()(注意,它返回一个Deferred)。您可以使用另一个reactor.listenTCP(或您第一次开始监听的API)调用重新开始侦听端口。在

相关问题 更多 >