Python requests“Max retries exceeded with url” error

created at 07-05-2021 views: 3

error1

NewConnectionError('\: Failed to establish a new connection:
[WinError 10060] The connection attempt failed because the connected party did not reply correctly after a period of time or the connected host did not respond. ',))

Solution

session.keep_alive=False

error2

python hostname doesn't match either of facebookXXXXX

solution

import ssl
ssl.match_hostname = lambda cert, hostname: True

After consulting many parties, I found the reason for the problem: too many http connections were not closed.

Solution:

  • Increase the number of connection retries
    requests.adapters.DEFAULT_RETRIES = 5
  • Close redundant connections

Requests uses the urllib3 library, the default http connection is keep-alive, and requests is set to False to close.

Method of operation

s = requests.session()
s.keep_alive = False

Only use session for operation. That is, only one connection is created, and the maximum number of connections or the number of retries is set.

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)
 import requests
   from requests.adapters import HTTPAdapter
   from requests.packages.urllib3.util.retry import Retry
   s = requests.Session()
   retry = Retry(connect = 5, backoff_factor = 1)
   adapter = HTTPAdapter(max_retries = retry)
   s.mount('http://', adapter)
   s.keep_alive = False
   res = s.post(self.conn.host + '/sign-in', data = json.dumps({
       'name': "XXX",
       'pwd': "XXX"
   }))
   response = res.json()

But someone gave such an explanation on starkoverflow.

1.Install py

pip install -U pyopenssl

2. Set a fixed sleep time between sending requests

https://github.com/requests/r...
https://stackoverflow.com/que...

Please log in to leave a comment.