简体   繁体   English

Python超时似乎不起作用

[英]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; 在正常情况下,当收到来自http://icanhazip.com的响应时,输出是这样的;

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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