[Twisted-Python] reconnecting from the client

Bosnjak Zoran ITWEP Bosnjak at iskratel.si
Mon Jul 26 11:13:37 EDT 2004

in my simple client I need a mechanism for reconnecting to the server:

1. If the server is not responding at the momment, the client should keep trying to connect
2. If the connection once become active and the server is restarted, the client should reconnect
3. If the connection is active and no data can be transmited / received for X seconds (link down, router down),
   the client should try to reconnect as in the case (1.) (as if the client has never been connected)
   It should react in less then 10 seconds.

Unfortunately I can not detect the situation (3.). If I unplug the ethernet while connected, none of my methods is called (ever).
I couldn't find anything like this in the documentation or in the examples.

Here is my testfile:
from twisted.internet.protocol import Protocol, ReconnectingClientFactory
from twisted.internet import reactor

class Echo(Protocol):
    def dataReceived(self, data):
        print data

    def connectionLost(self, reason):
        print 'connection lost', reason

    def connectionMade(self):
        reactor.callLater(1.0, self.ping)
        print 'connection made'

    def ping(self, *args):
        reactor.callLater(1.0, self.ping)

class EchoClientFactory(ReconnectingClientFactory):
    maxDelay = 3
    def startedConnecting(self, connector):
        print 'started to connect'

    def buildProtocol(self, addr):
        print 'connected'
        return Echo()

    def clientConnectionLost(self, connector, reason):
        print 'lost connection', reason
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

    def clientConnectionFailed(self, connector, reason):
        print 'connection failed', reason
        ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)

reactor.connectTCP('', 23, EchoClientFactory())


Zoran Bosnjak

More information about the Twisted-Python mailing list