Categories
Python Answers

How to fix max retries exceeded with URL in Python requests?

Spread the love

Sometimes, we want to fix max retries exceeded with URL in Python requests.

In this article, we’ll look at how to fix max retries exceeded with URL in Python requests.

How to fix max retries exceeded with URL in Python requests?

To fix max retries exceeded with URL in Python requests, we can set the retries when making a request with requests.

For instance, we write

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


session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

session.get(url)

to create a HTTPAdapter with the max_retries set to the Retry object.

We set the Retry to max 3 retries and backoff_factor is the delay between retries in seconds.

Then we call session.mount with adapter to use the retry settings.

And then we call session.get with url to make the GET request.

Conclusion

To fix max retries exceeded with URL in Python requests, we can set the retries when making a request with requests.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *