【问题标题】:Python Timeout doesn't seem to workPython 超时似乎不起作用
【发布时间】:2017-12-31 01:01:01
【问题描述】:

我有以下代码;

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"

在正常情况下,当收到来自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"

因此,当前日期和日志生成日期相同。 但是,当出现异常时,就会发生这种情况;

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"

为什么“超时”不起作用?

【问题讨论】:

  • date && tail -1 xxx.log 是自动运行的吗?因为否则时差有些无关紧要
  • 您可以记录异常以查看发生了什么:except Exception as err:public_ipv4 = "failed to retrieve public_ipv4; reason: " + str(err)
  • @dhdavvie:手动运行日期和尾部以检查时间戳。如果时间差超过 5 分钟,则会在“Splunk”中向这些日志馈送到的位置发出警报。
  • @VPfB:好的,我会试试的。谢谢。

标签: python python-2.7 subprocess


【解决方案1】:

尝试以这种方式获取详细的异常详细信息 然后调查到底是什么错误,时间上的差异 使用这种格式...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 2018-08-11
    • 2017-08-31
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    相关资源
    最近更新 更多