Python Requests Handle Errors
Python Requests is a popular library used for making HTTP requests in Python. It provides an easy-to-use interface for sending HTTP/1.1 requests using Python. When sending requests, it's important to handle errors properly. In this article, we will discuss how to handle errors when using the Python Requests library.
Handling Exceptions
When sending requests with Python Requests, you may encounter exceptions such as connection errors, timeout errors or invalid URL errors. To handle these exceptions, you can use the built-in Python exception handling mechanism.
import requests
try:
response = requests.get('https://www.example.com')
response.raise_for_status()
except requests.exceptions.RequestException as err:
print(err)
In the above code, we are sending a GET request to "https://www.example.com" and catching any exception using a try-except block. The raise_for_status()
method raises an HTTPError if the response status code is an error (4xx or 5xx). If there are any exceptions, the error message will be printed.
Status Codes
Status codes are a way for servers to communicate the success or failure of a request to the client. The Python Requests library provides an easy way to check the status code of a response.
import requests
response = requests.get('https://www.example.com')
if response.status_code == 200:
print('Success!')
else:
print('Failed with status code: ', response.status_code)
In the above code, we are sending a GET request to "https://www.example.com" and checking if the status code is 200 (OK). If it's not 200, then it will print the status code and indicate failure.
Retrying Requests
Sometimes, you may encounter temporary errors such as network issues or server errors. In such cases, you may want to retry the request after waiting for some time. The Python Requests library provides a built-in way to retry requests using the Retry object.
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
session = requests.Session()
retry = Retry(total=5, backoff_factor=1, status_forcelist=[ 500, 502, 503, 504 ])
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
try:
response = session.get('https://www.example.com')
response.raise_for_status()
except requests.exceptions.RequestException as err:
print(err)
In the above code, we are creating a session object and configuring the Retry object to retry a maximum of 5 times with a backoff factor of 1 second. We are also specifying a list of status codes that should be retried. We then mount the HTTPAdapter to the session object and send the GET request using the session object. If there are any exceptions, the error message will be printed.