Python requests“Max retries exceeded with url” error
19-4-29补充:
- error1:
NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000000038F2B00>: Failed to establish a new connection:
[WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',))
解决办法:
session.keep_alive=False
- error2:
python hostname doesn't match either of facebookXXXXX
解决办法:
import ssl
ssl.match_hostname = lambda cert, hostname: True
多方查阅后发现了解决问题的原因:http连接太多没有关闭导致的。
解决办法:
- 增加重试连接次数requests.adapters.DEFAULT_RETRIES = 5
- 关闭多余的连接
requests使用了urllib3库,默认的http connection是keep-alive的,requests设置False关闭。
操作方法
s = requests.session()
s.keep_alive = False
-
只用session进行操作。即只创建一个连接,并设置最大连接数或者重试次数。
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retrysession = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('<a>http://</a>', adapter)
session.mount('<a>https://</a>', 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()
但是在starkoverflow上有人给出了这样的解释。
- 安装 py
pip install -U pyopenssl
5、设定固定的睡眠时间在发送请求之间
https://github.com/requests/r...
https://stackoverflow.com/que...