python requests module error handling

Python Requests Module Error Handling

Handling errors in Python requests module is important to ensure that your program does not crash when it encounters an error. There are various types of errors that can occur when using the requests module, such as network errors, connection errors, and HTTP errors.

Network Errors

Network errors occur when there is an issue with the network connection. This may be due to a variety of reasons, such as a temporary outage, a firewall blocking the connection, or a DNS issue. To handle network errors, you can use a try-except block with the requests.exceptions.RequestException class. This class handles all exceptions that are raised by the requests module.


import requests

try:
    response = requests.get("https://www.example.com")
    response.raise_for_status()
except requests.exceptions.RequestException as e:
    print(e)
    

In the above code, we first make a GET request to the example website. We then use the raise_for_status() method to check if the request was successful. If an error occurred, the method will raise an HTTPError. We catch this error using the RequestException class and print out the error message.

HTTP Errors

HTTP errors occur when the server returns an error response code, such as 404 Not Found or 500 Internal Server Error. To handle HTTP errors, you can use the raise_for_status() method as shown above. This method raises an HTTPError if the status code is not in the 200-299 range.


import requests

try:
    response = requests.get("https://www.example.com/404")
    response.raise_for_status()
except requests.exceptions.HTTPError as e:
    print(e)
    

In the above code, we make a GET request to a non-existent page on the example website. The raise_for_status() method will raise an HTTPError since the status code is 404. We catch this error using the HTTPError class and print out the error message.

Connection Errors

Connection errors occur when there is an issue with the connection to the server. This may be due to a variety of reasons, such as a timeout, a DNS issue, or a firewall blocking the connection. To handle connection errors, you can use the ConnectionError class.


import requests

try:
    response = requests.get("https://www.example.com", timeout=0.1)
    response.raise_for_status()
except requests.exceptions.ConnectionError as e:
    print(e)
    

In the above code, we make a GET request to the example website with a timeout of 0.1 seconds. Since the timeout is too short, a ConnectionError is raised. We catch this error using the ConnectionError class and print out the error message.