Default Timeouts in Python urllib2

by Brian on 3/13/2009

If you are building a script to fetch data from websites (possible for web services) it is a good idea to specify timeouts so that your application does not hang.  In Python 2.5 this can be done like so:

import socket
import urllib2

socket.setdefaulttimeout(10)

This will make all calls to urllib2.urlopen() timeout after 10 seconds.  Note that in Python 2.6 you can specify the timeout in the call to urlopen().

Related posts:

  1. Python Telnet Code

Previous post:

Next post: