python requests max retries

Python Requests Max Retries

If you are working with Python Requests library, you might have encountered the error "Max retries exceeded with url" at some point in time. This error occurs when the requests library fails to establish a connection with the server after multiple attempts.

Cause of Error

This error is generally caused due to one of the following reasons:

  • Server is temporarily down or not responding
  • Slow internet connection
  • Incorrect URL or endpoint

Solution

To resolve this error, you can implement one of the following solutions:

1. Increase Max Retries

You can increase the maximum number of retries using the "max_retries" parameter. By default, this parameter is set to 0, which means that there will be no retries. You can set it to a higher number to increase the number of retries.


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

retry_strategy = Retry(
    total=5,
    status_forcelist=[ 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)

response = http.get("https://www.example.com")
print(response.content)

2. Change Timeout Value

You can also increase the timeout value for establishing a connection with the server. By default, the timeout value is set to None, which means that the request will wait indefinitely for a response. You can set it to a higher value to increase the timeout duration.


import requests

response = requests.get("https://www.example.com", timeout=10)
print(response.content)

3. Check Internet Connection

If you are facing this error frequently, you might want to check your internet connection. Slow or intermittent internet connection can cause this error.

4. Check URL or Endpoint

Make sure that you are using the correct URL or endpoint. Incorrect URL or endpoint can also cause this error.

By implementing one of the above solutions, you should be able to resolve the "Max retries exceeded with url" error in Python Requests library.