Note that there are some explanatory texts on larger screens.

plurals
  1. POPython socket error resilience / workaround
    primarykey
    data
    text
    <p>I have a script running that is testing a series of urls for availability.</p> <p>This is one of the functions.</p> <pre><code>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 </code></pre> <p>Occasionally, the VPS will lose connectivity, the entire script crashes when that occurs.</p> <pre><code>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 </code></pre> <p>I'm not at all familiar with handling errors like this in python.</p> <p>What is the appropriate way to keep the script from crashing when network connectivity is temporarily lost?</p> <hr> <p>Edit:</p> <p>I ended up with this - feedback?</p> <pre><code>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 </code></pre> <p>I'm not sure I fully understand what raise does though..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload