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. ',))
session.keep_alive=False
python hostname doesn't match either of facebookXXXXX
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:
requests.adapters.DEFAULT_RETRIES = 5
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...