" />

mikerochford.com Being technical

7Jan/150

Setting a timeout for urllib2 in python2.4

Wait, Did I really say python2.4? Yes, Yes, I did.

Since the timeout argument doesn't exist in python2.4 urllib2.urlopen you will need to override the socket variable manually.

Example:

#!/usr/bin/env python

import socket
import urllib2

url = 'http://www.google.com'

socket.setdefaulttimeout(10)
try:
    request = urllib2.urlopen(url)
except:
    raise
else:
    data = request.read()