I'd like to end prematurely a call to urllib2.urlopen() if DNS resolution fails. Calling urllib2.urlopen(url, timeout) timeouts only if the network is up. If network is down it waits for 30 seconds because it can't resolve url. If you use IP instead of DNS, timeout value is being respected.
Using:
signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(2) urllib2.urlopen(url, 2) signal.alarm(0)
does't work. If url is a FQDN then it stucks for 30 seconds on urllib2.urlopen.