[urllib2] No time-out?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Nov 16 06:39:54 EST 2008


On Sun, 16 Nov 2008 12:04:02 +0100, Gilles Ganault wrote:

> Hello
> 
> I'm using urllib2 to download web pages. The strange thing in the code
> below, is that it seems like urllib2.urlopen retries indefinitely by
> itself instead of raising an exception:

Try this instead (untested):

timeout = 30
socket.setdefaulttimeout(timeout)
url = 'http://www.acme.com'
for i in range(5):
    try:
	print url
        req = urllib2.Request(url, None, headers)
        response = urllib2.urlopen(req).read()
        break
    except urllib2.URLError:
        print "Timed-out."
        time.sleep(10)
else:
    print "Exiting."
    connection.close(True)  # What is this?
    sys.exit()  # Do you really want the application to exit?



-- 
Steven



More information about the Python-list mailing list