How to Fix “ConnectionError: Max retries exceeded with url” in Python?

There are multiple errors that appear while programming in Python such as SyntaxError, Valueerror, IndexError, etc. One such error is “ConnectionError” which arises in Python while working with the “request” module. To rectify this particular error, various solutions are used in Python.

In this Python post, we will explain the reason and solutions for “ConnectionError: Max retries exceeded with URL”  using suitable examples. This post will explore the following content for a profound understanding of the concept:

Reason: Unable to Establish a Connection to a Specified URL

The stated error occurs when a Python script cannot establish a connection to a specified URL after a certain number of retries. The error message states “failed to establish a connection” because of exceeding the maximum number of retries in a program. The stated error may also occur because of network connectivity issues, down servers, or incorrect URLs.

The above snippet shows that the “ConnectionError” has been returned in the output.

Solution 1: Check the URL

To resolve this error you need to make sure that the URL you are trying to connect to is correct and reachable. We can check this specified URL by accessing it in a web browser. If the specified URL is incorrect, then you must correct it. In such a case, correcting the specified URL will resolve the stated error.

Code: 

import requests
url = 'https://www.itslinuxfoss.com'
response = requests.get(url)
print(response)

The “requests” module “requests.get()” function is used to make the connection to the specified URL.

Output: 

The above output confirmed that the connection has been established.

Solution 2: Use a Retry Object

We can also resolve the “Connection Error” by increasing the number of retries in the Python script. The retry object method will give more chances to establish a connection to a specified URL.

Code:

import requests
from requests.adapters import HTTPAdapter
specified_url = "https://www.itslinuxfoss.com"
session = requests.Session()
retry = HTTPAdapter(max_retries=5)
session.mount("http://", retry)
session.mount("https://", retry)
response = session.get(specified_url)
print(response)

The “request” module’s function “HTTPAdapter()” accepts the “max_retries” parameter to increase the number of retries.

Output:

The connection has been built successfully.

Solution 3: Use Try-Except

The error can also be fixed and handled using the “Try-Except” block. This method helps us to continue running our script even if it encounters the “ConnectionError: Max retries exceeded with url” error.

Code:

import requests

specified_url = "https://www.itslinuxfoss.com"
try:
    response = requests.get(specified_url)
    response.raise_for_status()
    print(response)
except requests.exceptions.RequestException:
    print("Request failed")

The “try” block executes the code and makes the connection to the URL. But if the connection is not established then the “except” block will run.

Output:

The above output proved that the connection has been established successfully.

Solution 4: Use a Stable Internet Connection

Connectivity issues can also cause this error in Python, so we can rectify this error by using a stable internet connection.

Conclusion

To resolve the ConnectionError error, various solutions are used in Python, such as checking the URL, using a retry object, using a Try-Except block, and maintaining a Stable internet connection. The URL must be double-checked because if the URL is not reachable then the “ConnectionError” appears in the script. The increasing number of retries in Python script and using a try-except block will increase the chances of a successful connection. This guide presented various solutions on how to resolve the error named“ConnectionError: Max retries exceeded with url” in Python.