2

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?

SiHa
  • 7,830
  • 13
  • 34
  • 43
  • is that `date && tail -1 xxx.log` run automatically? Because otherwise the time difference is somewhat irrelevant – dheiberg Jul 25 '17 at 08:31
  • You could log the exception to see what's going on: `except Exception as err:` and `public_ipv4 = "failed to retrieve public_ipv4; reason: " + str(err)` – VPfB Jul 25 '17 at 08:34
  • @dhdavvie: The date & tail was run manually to check the timestamp. If the time difference is more than 5 minutes, it causes an alert in "Splunk" to where these logs are fed to. – Sourav Mishra Jul 25 '17 at 08:42
  • @VPfB: Ok will try that out. Thanks. – Sourav Mishra Jul 25 '17 at 08:43
  • Possible duplicate of [Python urllib2 does not respect timeout](https://stackoverflow.com/questions/27327787/python-urllib2-does-not-respect-timeout) – Amin Etesamian Jul 25 '17 at 10:19

1 Answers1

0

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()
Dibsyhex
  • 119
  • 7