MjjKing 大佬有话说 :
亲测PaChong重试方法的一个坑
本帖最后由 MjjKing 于 2021-8-4 13:54 编辑
看网上说python requests有两种设置网页最大尝试次数的方式(http://www.xoxxoo.com/index.php/index/index/article/id/775),项目中试了下不尽人意,故而测测,发现如下:
requests.adapters.DEFAULT_RETRIES = 5 参数不管是单独使用,还是配合Session都是无效的。
有效的只有:
s.mount(‘http://’, HTTPAdapter(max_retries=5))
s.mount(‘https://’, HTTPAdapter(max_retries=5))
不对的地方请大佬指正哈
https://i.loli.net/2021/08/04/zqoS95uJHZ8cIpr.png
https://i.loli.net/2021/08/04/KmgeMsOqlyTYj6r.png
https://i.loli.net/2021/08/04/WwjVYiCb3HKardq.png
还有一个就是timeout设置的过短,如果设置5秒,第一个请求还没结束就会发起下一个请求,暂时不清楚如果第二个请求开启了,但第一个请求服务端响应成功了会造成什么影响
https://i.loli.net/2021/08/04/fQWq6g8mCHDOwMs.png
https://i.loli.net/2021/08/04/4eSc1U3j27xkrAO.png
海苔 大佬有话说 :
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
def postData(url, data, headers=None, retry=5, timeout=10):
retry_strategy = Retry(total=retry, backoff_factor=0.1)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)
response = http.post(url, data=data, headers=headers, timeout=timeout)
return response
这样即可
我是坏虫 大佬有话说 :
;P看不懂https://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif
wifitry 大佬有话说 :
不明觉厉
MjjKing 大佬有话说 :
海苔 大佬有话说 : 2021-8-4 13:47
这样即可
大佬,你这个我试了试,也是没重试。。。。
https://i.loli.net/2021/08/04/kFqlzUcLtCx6vNY.png
海苔 大佬有话说 :
MjjKing 大佬有话说 : 2021-8-4 14:08
大佬,你这个我试了试,也是没重试。。。。
502重试要在Retry里面加参数
status_forcelist=[ 502, 503, 504 ]https://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif
MjjKing 大佬有话说 :
海苔 大佬有话说 : 2021-8-4 14:24
502重试要在Retry里面加参数
哦哦,这个参数在http.post的参数名是啥
海苔 大佬有话说 :
MjjKing 大佬有话说 : 2021-8-4 14:27
哦哦,这个参数在http.post的参数名是啥
retries = Retry(total=5, backoff_factor=1, status_forcelist=[ 502, 503, 504 ])
这样https://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif
MjjKing 大佬有话说 :
海苔 大佬有话说 : 2021-8-4 14:28
这样
试了试也是不行。yc003t