urllib2.urlopen timeout

Ben Charrow bcharrow at csail.mit.edu
Mon Aug 3 20:26:38 CEST 2009


Zdenek Maxa wrote:
> Hi,
> 
> I would like to ask how I should set timeout for a call:
> 
> f = urllib2.urlopen(url)
> 
<snip>
> 
> I know that Python 2.6 offers
> urllib2.urlopen(url[, data][, timeout])
> which would elegantly solved my problem, but I have to stick to Python 2.5.
> 

There are three solutions that I know about:
1) Make your own little HTTP library and set timeouts for each of your sockets.
2) The same as the above, except use asynchronous sockets and the the select
module.
3) If you're on a Unix style system, use the signal module (the example is
helpful http://docs.python.org/library/signal.html#example)

Here's some code:

import urllib2
import signal
class TimeoutException(Exception):
    """SIGALARM was sent to the process"""
    pass

def raise_timeout(signum, frame):
    raise TimeoutException("Timeout!")

signal.signal(signal.SIGALRM, raise_timeout)

try:
    signal.alarm(5) # raise alarm in 5 seconds
    data = urllib2.urlopen("http://www.google.com").readlines()
except TimeoutException, ex:
    data = None
finally:
    signal.alarm(0) # disable alarm

HTH,
Ben



More information about the Python-list mailing list