“ConnectionError: Max retries exceeded with url” is a common error in Python related to a connection error. The below explanations can help you know more about the cause of this error and solutions.
How does the error “Max retries exceeded with URL” happen?
“Max retries exceeded with URL” is a common error related to a connection error. You will encounter this situation while making requests using the requests library.
This error indicates that the request could not be completed successfully. There can be no more connections since the server is overloaded. You need to verify that you can connect to the device causing the problems. There are five common cases:
- The host is not listening on the port given
- There are too many open HTTPconnections.
- There is not enough RAM on the computer.
- Another option is that the target website has blacklisted the IP as a result of the frequent requests.
- The requested URL is incorrect.
In general, the detailed error message should resemble the following output. The word “requests.exceptions.ConnectionError” normally appears at the beginning of an error message, informing us that something went wrong when the request was attempting to connect. There are hence no general solutions because it depends on your error message.
The exception “requests.exceptions.SSLError” can occasionally appear, which is obviously an SSL-related problem. The error comes with a longer message text, potentially something like this: Unable to establish a new connection: Connection rejected or [Errno -2] Unknown name or service are also errors. These messages allow us to further identify and address the problem.
[Errno 61] Connection refused
import requests response = requests.get("https://eu.httpbin.org/") print(response.status_code)
The fact that the website eu.httpbin.org is not a secure HTTPS connection, and port 443 is of HTTPS, while port 80 is instead an HTTP one. One possible reason why this would happen is if the connection was attempted to port 443, but the host was not listening to it. In this case, consider listening on port 80 instead.
import requests response = requests.get("http://eu.httpbin.org/") print(response.status_code)
[Errno -2] Name or service not known
import requests from lxml import html page = requests.get("https://learnshareit.com") tree = html.fromstring(page.text)
Your connection is being denied by the LearnshareIT server at this time (you had sent too many requests from the same IP address in a short period of time). The error log can be understood as “The connection could not be established as the intended machine actively rejected it. To fix this, please read the two approaches below.
How to solve the error “Max retries exceeded with URL”?
Method 1: Let enough time between queries to the server
You must let enough time for queries to the server, which may be done using Python’s
sleep() (time in sec) function (remember to import sleep)
import time page = '' while page == '': try: page = requests.get("https://learnshareit.com") break except: print("Error.Waiting 6s") time.sleep(6) print("Now try reconnecting") continue
Method 2: Disable SSL verification
Another way to fix this problem is to disable SSL verification. You should also use try catch exceptions like 1st method to make sure not to send too many requests:
import requests page = requests.get("https://learnshareit.com", verify=False)
The error ConnectionError: Max retries exceeded with url occurs when too many failed requests have been made. To help with this situation, make sure you are connecting to the correct URL and remember to leave a sleep time for your requests before continuing to retry.
Maybe you are interested in similar errors:
- RuntimeError: dictionary changed size during iteration
- TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’
- JSONDecodeError: Expecting value: line 1 column 1 (char 0)
- UnicodeEncodeError: ‘ascii’ codec can’t encode character in position
- SyntaxError: Missing parentheses in call to ‘print’ in Python
- DataFrame constructor not properly called!
Name of the university: HCMUT