Python requests“Max retries exceeded with url” error
1、增加重试连接次数
requests.adapters.DEFAULT_RETRIES = 5
2、关闭多余的连接
requests使用了urllib3库,默认的http connection是keep-alive的,requests设置False关闭。
操作方法
s = requests.session()
s.keep_alive = False
3、只用session进行操作。即只创建一个连接,并设置最大连接数或者重试次数。
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()
但是在starkoverflow上有人给出了这样的解释。
4.安装 py
pip install -U pyopenssl
5、设定固定的睡眠时间在发送请求之间
https://github.com/requests/requests/issues/4246#event
https://stackoverflow.com/questions/23013220/max-retries-exceeded-with-url
作者:CoderPan
链接:https://www.jianshu.com/p/442cb3efe347

浙公网安备 33010602011771号