[Tutor] Python urllib (linux)

Terry Carroll carroll at tjc.com
Fri Jun 30 19:47:22 CEST 2006


On Fri, 30 Jun 2006, Patty wrote:

> I'm doing some failure testing for a python script that uses urllib and
> urllib2 to open a web page and post data.... Is there a way I can
> implement a timeout to handle this problem?

I don't have any firsth-hand knowledge on this, but 
http://www.voidspace.org.uk/python/articles/urllib2.shtml says:

------------------------------------
By default the socket module has no timeout and can hang. Currently, the 
socket timeout is not exposed at the httplib or urllib2 levels. However, 
you can set the default timeout globally for all sockets using :

import socket
import urllib2

# timeout in seconds
timeout = 10
socket.setdefaulttimeout(timeout)

# this call to urllib2.urlopen now uses the default timeout
# we have set in the socket module
req = urllib2.Request('http://www.voidspace.org.uk')
response = urllib2.urlopen(req)
------------------------------------

Good luck!



More information about the Tutor mailing list