简体   繁体   English

如何在Twisted中关闭和启动服务器?

[英]How to shutdown and start a server in Python Twisted?

My client (based on twisted) is supposed to automatically reconnect to server when connection is lost, i need to make a test of this feature, here's my test method where the @todo comment is very clear about what behaviour is expected: 我的客户端(基于双绞线)应该在连接断开时自动重新连接到服务器,我需要对此功能进行测试,这是我的测试方法,其中@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)

On the server side, am trying to abort connect just after receiving a bindAsTransmitter request: 在服务器端,正试图在收到bindAsTransmitter请求后中止连接:

class LooseConnectionOnBindSMSC(SMSC):

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

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

The connection is successfully aborted, my client starts trying to reconnect as expected but it never get the way to get my server UP again. 连接已成功中止,我的客户端开始尝试按预期重新连接,但它始终无法使服务器重新启动。

Your server is still running (as far as anyone can tell from the code in your question). 您的服务器仍在运行(据任何人都可以从您问题中的代码看出来)。 Closing one connection to a client doesn't stop the server from accepting new connections. 断开与客户端的一个连接并不会阻止服务器接受新连接。

The way to stop a listening port from listening is with port.stopListening() (note that it returns a Deferred ). 阻止监听端口监听的方法是使用port.stopListening() (请注意,它返回Deferred )。 You can start listening on the port again with another reactor.listenTCP (or whichever API you used to start listening the first time) call. 您可以使用另一个reactor.listenTCP (或您第一次开始监听的API)再次开始监听该端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM