I've just inherited some Python code and need to fix a bug as soon as possible.
I have very little Python knowledge so please excuse my ignorance.
I am using urllib2
to extract data from web pages.
Despite using socket.setdefaulttimeout(30)
I am still coming across URLs that hang seemingly indefinitely.
I want to time out the extraction and have got this far after much searching the web:
import socket
socket.setdefaulttimeout(30)
reqdata = urllib2.Request(urltocollect)
def handler(reqdata):
???? reqdata.close() ????
t = Timer(5.0, handler,[reqdata])
t.start()
urldata = urllib2.urlopen(reqdata)
t.cancel()
The handler function triggers after the time has passed but I don't know how to get it to stop the openurl operation.
Any guidance would be gratefully received. C
UPDATE ------------------------- In my experience when used on certain URLs urllib2.urlopen hangs and waits indefinitely. The URLs that do this are ones that when pointed to with a browser never resolve, the browser just waits with the activity indicator moving but never connecting fully. I suspect that these URLs may be stuck inside some kind of infinite looping redirect. The timeout argument to urlopen (in later versions of Python) and the socket.setdefaulttimeout() global setting do not detect this issue on my system.
I tried a number of solutions but in the end I updraded to Python 2.7 and used a variation of Werner’s answer below. Thanks Werner.