retry in requests

Retry in Requests

If you are working with APIs or web scraping, you might have encountered situations where the request fails due to poor internet connection or server errors. In such cases, you can retry the request instead of completely giving up the task.

How to implement retry in Requests?

Requests is a popular Python library used for making HTTP requests. It allows you to specify the number of times you want to retry the request and the time gap between each retry. Here's an example code:


import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

# Define the retry strategy
retry_strategy = Retry(
    total=3,
    backoff_factor=2,
    status_forcelist=[429, 500, 502, 503, 504],
    method_whitelist=["HEAD", "GET", "OPTIONS"]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)

# Make the request
response = http.get("https://example.com")

In this example, we have defined a retry strategy that retries the request up to 3 times with an increasing time gap between each retry. We have also specified a list of HTTP status codes for which we want to retry the request. The max_retries parameter specifies the maximum number of retries. We have used the HTTPAdapter class to attach the retry strategy to our Session object.

Other Ways to Implement Retry in Requests

  • You can use a decorator to wrap the function that makes the request and retry it if it fails.
  • You can use a third-party library like Tenacity that provides more advanced retry strategies.

Retry is a useful technique that can help you handle errors more gracefully and improve the overall robustness of your code.