简体   繁体   中英

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:

@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:

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 ). You can start listening on the port again with another reactor.listenTCP (or whichever API you used to start listening the first time) call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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