[Twisted-Python] TCP client that can connect to a server at periodic intervals

Gaspard, Bradley S BRADLEY.S.GASPARD at saic.com
Mon Mar 5 12:58:50 EST 2007

I am trying to write a TCP client that at regular intervals connects to a server makes a request and then closes the connection. Below is the code I've been playing with. I've seen an example of LoopingCall used for periodically sending UPD packets but am not sure how it would apply here because it would seem I need to periodically create new Protocol instances in the ClientFactory class. I've looked for examples and through the documentation but have come up empty. Any pointers would be greatly appreceiated.

class AnnouncementFetcher(protocol.Protocol):

    def connectionMade(self):
        print "connectionMade called"

    def dataReceived(self, data):
        print "Received: ", repr(data)
        # self.factory.share.saveData(data)

class AnnouncementFetcherFactory(protocol.ClientFactory):
    protocol = AnnouncementFetcher

    def __init__(self, share):
        self.share = share

    def buildProtocol(self, addr):
        print 'Connected.'
        p = AnnouncementFetcher()
        p.factory = self
        return p

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection,  Reason:', reason
        reactor.callLater(5, self.buildProtocol, 'localhost')

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20070305/f6c63222/attachment.htm 

More information about the Twisted-Python mailing list