Correct way to try/except using Python requests module? Correct way to try/except using Python requests module? python python

Correct way to try/except using Python requests module?


Have a look at the Requests exception docs. In short:

In the event of a network problem (e.g. DNS failure, refused connection, etc), Requests will raise a ConnectionError exception.

In the event of the rare invalid HTTP response, Requests will raise an HTTPError exception.

If a request times out, a Timeout exception is raised.

If a request exceeds the configured number of maximum redirections, a TooManyRedirects exception is raised.

All exceptions that Requests explicitly raises inherit from requests.exceptions.RequestException.

To answer your question, what you show will not cover all of your bases. You'll only catch connection-related errors, not ones that time out.

What to do when you catch the exception is really up to the design of your script/program. Is it acceptable to exit? Can you go on and try again? If the error is catastrophic and you can't go on, then yes, you may abort your program by raising SystemExit (a nice way to both print an error and call sys.exit).

You can either catch the base-class exception, which will handle all cases:

try:    r = requests.get(url, params={'s': thing})except requests.exceptions.RequestException as e:  # This is the correct syntax    raise SystemExit(e)

Or you can catch them separately and do different things.

try:    r = requests.get(url, params={'s': thing})except requests.exceptions.Timeout:    # Maybe set up for a retry, or continue in a retry loopexcept requests.exceptions.TooManyRedirects:    # Tell the user their URL was bad and try a different oneexcept requests.exceptions.RequestException as e:    # catastrophic error. bail.    raise SystemExit(e)

As Christian pointed out:

If you want http errors (e.g. 401 Unauthorized) to raise exceptions, you can call Response.raise_for_status. That will raise an HTTPError, if the response was an http error.

An example:

try:    r = requests.get('http://www.google.com/nothere')    r.raise_for_status()except requests.exceptions.HTTPError as err:    raise SystemExit(err)

Will print:

404 Client Error: Not Found for url: http://www.google.com/nothere


One additional suggestion to be explicit. It seems best to go from specific to general down the stack of errors to get the desired error to be caught, so the specific ones don't get masked by the general one.

url='http://www.google.com/blahblah'try:    r = requests.get(url,timeout=3)    r.raise_for_status()except requests.exceptions.HTTPError as errh:    print ("Http Error:",errh)except requests.exceptions.ConnectionError as errc:    print ("Error Connecting:",errc)except requests.exceptions.Timeout as errt:    print ("Timeout Error:",errt)except requests.exceptions.RequestException as err:    print ("OOps: Something Else",err)Http Error: 404 Client Error: Not Found for url: http://www.google.com/blahblah

vs

url='http://www.google.com/blahblah'try:    r = requests.get(url,timeout=3)    r.raise_for_status()except requests.exceptions.RequestException as err:    print ("OOps: Something Else",err)except requests.exceptions.HTTPError as errh:    print ("Http Error:",errh)except requests.exceptions.ConnectionError as errc:    print ("Error Connecting:",errc)except requests.exceptions.Timeout as errt:    print ("Timeout Error:",errt)     OOps: Something Else 404 Client Error: Not Found for url: http://www.google.com/blahblah


Exception object also contains original response e.response, that could be useful if need to see error body in response from the server. For example:

try:    r = requests.post('somerestapi.com/post-here', data={'birthday': '9/9/3999'})    r.raise_for_status()except requests.exceptions.HTTPError as e:    print (e.response.text)