I'm writing a system monitor script that needs to load web pages. I'm
using urllib2.urlopen to get the pages, and I'm attempting to set the
timeout value using socket.defaultt imeout.
Specifically, I'm calling socket.defaultt timeout(10), then calling
urllib2.urlopen to fetch a web page that never gets delivered. My code
waits about 30 seconds before terminating.
I am about to add threading to my app so that delays on a few servers
won't be a critical issue, but I'd still like to understand why the call
to socket.defaultt imeout doesn't affect the timeout on my urlopen calls.
My code follows.
#!/usr/local/bin/python
import socket, time
socket.setdefau lttimeout(10)
import urllib2
def doHttpTest():
url = "http://url.that.never. returns"
t_start = time.time()
if httptest(url):
print "Error on site ", url
t_end = time.time()
t_diff = t_end - t_start
def httptest(url):
timeout = 10
socket.setdefau lttimeout(timeo ut)
try:
req = urllib2.Request (url)
urllib2.urlopen (req)
except urllib2.HTTPErr or, e:
if e.code == 401:
return 1
elif e.code == 404:
return 1
elif e.code == 503:
return 1
else:
return 1
except urllib2.URLErro r, e:
return 1
else:
return 0
if __name__ == '__main__':
try:
doHttpTest()
except KeyboardInterru pt:
print "Exiting... "