Search code examples
pythonpython-2.7timeouturllib2raspbian

urllib2.urlopen timeout works only when connected to Internet


I'm working on Python 2.7 code to read a value from HTML page by using urllib2 library. I want to timeout the urllib2.urlopen function after 5 seconds in case of no Internet and jump to remaining code.

It works as expected when computer is connected to working internet connection. And for testing if I set timeout=0.1 it timed out suddenly without opening url, as expected. But when there is no Internet, timeout not works either I set timeout to 0.1, 5, or any other value. It simply does not timed out.

This is my Code:

import urllib2
url = "https://alfahd.witorbit.net/fingerprint.php?n"
try:
    response = urllib2.urlopen(url , timeout=5).read()
    print response 
except Exception as e:
    print e

Result when connected to Internet with timeout value 5:

180

Result when connected to Internet with timeout value 0.1 :

<urlopen error timed out>

Seems timeout is working.

Result when NOT connected to Internet and with any timeout value (it timed out after about 40 seconds every time I open url despite of any value I set for timeout=:

<urlopen error [Errno -3] Temporary failure in name resolution>

How can I timeout urllib2.urlopen when there is no Internet connectivity? Am I missing some thing? Please guide me to solve this issue. Thanks!


Solution

  • Because name resolution happens before the request is made, it's not subject to the timeout. You can prevent this error in name resolution by providing the IP for the host in your /etc/hosts file. For example, if the host is subdomain.example.com and the IP is 10.10.10.10 you would add the following line in the /etc/hosts file

    10.10.10.10    subdomain.example.com
    

    Alternatively, you may be able to simply use the IP address directly, however, some webservers require you use the hostname, in which case you'll need to modify the hosts file to use the name offline.