Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python socket error resilience / workaround

I have a script running that is testing a series of urls for availability.

This is one of the functions.

def checkUrl(url): # Only downloads headers, returns status code.
    p = urlparse(url)
    conn = httplib.HTTPConnection(p.netloc)
    conn.request('HEAD', p.path)
    resp = conn.getresponse()
    return resp.status

Occasionally, the VPS will lose connectivity, the entire script crashes when that occurs.

File "/usr/lib/python2.6/httplib.py", line 914, in request
  self._send_request(method, url, body, headers)
File "/usr/lib/python2.6/httplib.py", line 951, in _send_request
  self.endheaders()
File "/usr/lib/python2.6/httplib.py", line 908, in endheaders
  self._send_output()
File "/usr/lib/python2.6/httplib.py", line 780, in _send_output
  self.send(msg)
File "/usr/lib/python2.6/httplib.py", line 739, in send
  self.connect()
File "/usr/lib/python2.6/httplib.py", line 720, in connect
  self.timeout)
File "/usr/lib/python2.6/socket.py", line 561, in create_connection
  raise error, msg
socket.error: [Errno 101] Network is unreachable

I'm not at all familiar with handling errors like this in python.

What is the appropriate way to keep the script from crashing when network connectivity is temporarily lost?


Edit:

I ended up with this - feedback?

def checkUrl(url): # Only downloads headers, returns status code.
    try:
        p = urlparse(url)
        conn = httplib.HTTPConnection(p.netloc)
        conn.request('HEAD', p.path)
        resp = conn.getresponse()
        return resp.status
    except IOError, e:
        if e.errno == 101:
            print "Network Error"
            time.sleep(1)
            checkUrl(url)
        else:
            raise

I'm not sure I fully understand what raise does though..

like image 274
some1 Avatar asked Aug 27 '26 02:08

some1


1 Answers

If you just want to handle this Network is unreachable 101, and let other exceptions throw an error, you can do following for example.

from errno import ENETUNREACH

try:
    # tricky code goes here

except IOError as e:
    # an IOError exception occurred (socket.error is a subclass)
    if e.errno == ENETUNREACH:
        # now we had the error code 101, network unreachable
        do_some_recovery
    else:
        # other exceptions we reraise again
        raise
like image 159


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!