Are you facing the error “ConnectionError: Max retries exceeded with URL“? Yes, you have come to the right place. Today I will show you how to solve this error.
[Fixed]: max retries exceeded with url
To solve the error “max retries exceeded with URL python” you need to use the ‘Retry‘ object and specify how many connection-related errors to retry on, and set a backoff factor to apply between attempts.
import requests
from requests.adapters import HTTPAdapter, Retry
def get_content():
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
url = 'https://www.totitech.org'
response = session.get(url)
content_json = response.json()
print(content_json)
if __name__ == "__main__:
get_content()
The ‘Session‘ object allows us to pass some parameters with requests. In the above code, we passed two arguments to the ‘Retry‘ object.
[Alternative Solution]: max retries exceeded with url python
An alternative solution to this problem is to add ‘try/except’ to get rid of this error. But this method will not retry but will simply pass the error.
import requests
def make_request():
try:
url = 'https://www.totitech.org'
response = requests.get(url)
get_json = response.json()
print(get_json)
except requests.exceptions.ConnectionError:
pass
#print('connection error occurred')
if __name__ == "__main__":
make_request()
‘Requests’ was unable to verify the SSL certificate of the site:
If you getting this error “‘Requests’ was unable to verify the SSL certificate of the site” you solve this error with an argument “verify = ‘false’” to bypass SSL certificate problem with requests.
You can pass “verify” like this:
import requests
def request_content():
try:
url = 'https://www.totitech.org'
# set verify to False to bypass SSL
response = requests.get(url, verify=False)
json_response = response.json()
print(json_response)
except Exception as e:
print(e)
if __name__ == "__main__":
request_content()
Conclusion on python max retries exceeded with URL
Programmers, we have discussed how to solve this error “python-requests max retries exceeded with url newconnectionerror“. If you are still getting the error please let us know in the comments section.
Leave a Reply