如何解决Python请求限制了新的连接尝试
我正在尝试限制request.Session()尝试连接到URL \设备的次数。我有以下内容:
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
session = requests.Session()
retry = Retry(total=0,read=0,connect=0,backoff_factor=0,status_forcelist=None)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://',adapter)
session.mount('https://',adapter)
session.get(url='http://192.168.0.10/ssi.cgi/Login.htm',timeout=(3,10))
这适用于大多数URL \设备,但是有时该设备没有响应,我遇到了以下日志:
Starting new HTTP connection (1): 192.168.0.10:80
Starting new HTTP connection (1): 192.168.0.10:80
Starting new HTTP connection (1): 192.168.0.10:80
Starting new HTTP connection (1): 192.168.0.10:80
Starting new HTTP connection (1): 192.168.0.10:80
Starting new HTTP connection (1): 192.168.0.10:80
ConnectionError
HTTPConnectionPool(host='192.168.0.10',port=80): Max retries exceeded with ur
l: /ssi.cgi/Login.htm (Caused by NewConnectionError('<urllib3.connection.HTTPCon
nection object at 0x02F11D50>: Failed to establish a new connection: [WinError 1
0060] A connection attempt failed because the connected party did not properly r
espond after a period of time,or established connection failed because connecte
d host has failed to respond'))
由于重试限制在这里无效,我该如何限制请求模块进行的“开始新的HTTP连接”尝试次数?
通常情况下,我会使用timeout =(15,60),但是连接将等待整整60秒再尝试一次,然后再执行5次...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。