[Twisted-Python] implementing a timeout

J Davis mrsalty0 at gmail.com
Thu Nov 29 16:45:27 MST 2007


Greetings,

I'm a newcomer to twisted and am struggling a little with the learning
curve.
I have an idea for an app that I believe is ideally suited for twisted
though I
could use some help finding the right way to approach it.

It performs a status check by getting a page from a web server.
It calculates the time it took to download the page and also can check that
the
content matches a predefined string.

That part of it is working fine but I'd like to take it a step further.
I need a way to impose a limit on how long it waits to receive a response so
that I can detect
unresponsive hosts. I must say that I'm at a loss as to how to add such a
timeout to my code.
Any pointers or examples would be greatly appreciated.

Here is a simplified version of the function that I'm working with.
---------------------------------------

from twisted.internet import task, reactor
from twisted.web import client
import time

class HTCheck:
    def handleResponse(self, data, start_time):
        response_time = time.time() - start_time
        if data == "It lives!\n":
            Status = "OK"
        else:
            Status = "Test page content mismatch"
    def check(self):
        start_time = time.time()
        d = client.getPage("http://localhost/test.html")
        d.addCallback(self.handleResponse, start_time)
        return d

l = task.LoopingCall(HTCheck().check)
l.start(10.0)
reactor.run()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20071129/ff5f98d7/attachment.html>


More information about the Twisted-Python mailing list