Python requests - Exception Type: ConnectionError - try: except does not work

connection, python, python-2.x, python-requests, url

Solution

You should not exit your worker instance `sys.exit(1)` Furthermore you 're catching the wrong Error.

What you could do for for example is:

from requests.exceptions import ConnectionError
try:
   r = requests.get("http://example.com", timeout=0.001)
except ConnectionError as e:    # This is the correct syntax
   print e
   r = "No response"

In this case your program will continue, setting the value of `r` which usually saves the response to any default value

Problem

I am using a webservice to retrieve some data but sometimes the url is not working and my site is not loading. Do you know how I can handle the following exception so there is no problem with the site in case the webservice is not working? ``` Django Version: 1.3.1 Exception Type: ConnectionError Exception Value: HTTPConnectionPool(host='test.com', port=8580): Max retries exceeded with url: ``` I used ``` try: r = requests.get("http://test.com", timeout=0.001) except requests.exceptions.RequestException as e: # This is the correct syntax print e sys.exit(1) ``` but nothing happens

Original source

Related problems