简体   繁体   中英

Python Timeout doesn't seem to work

I have the following code;

def ip_addresses():
    # Get external ipv4
    try:
        response = urllib2.urlopen('http://icanhazip.com', timeout = 2)
        out = response.read()
        public_ipv4 = re.sub('\n', '', out)
    except:
        public_ipv4 = "failed to retrieve public_ipv4"

In normal circumstance, when response from http://icanhazip.com is received, the output is something like this;

xxx@xxx:/var/log$ date && tail -1 xxx.log
Tue Jul 25 **07:43**:18 UTC 2017  {"public_ipv4": "208.185.193.131"}, "date": "2017-07-25 **07:43**:01.558242"

So, the current date and the date of the log generation are same. However, when there is an exception, this is happening;

xxx@xxx:/var/log$ date && tail -1 xxx.log
Tue Jul 25 **07:30**:25 UTC 2017  {"public_ipv4": "failed to retrieve public_ipv4"},"date": "2017-07-25 **07:23**:01.525444"

Why is the "timeout" not working?

Try to get the verbose exception details in this manner and then investigate what is the error all about, the difference in time Use this format...

import sys
try:
    1 / 0
except:
    print sys.exc_info()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM