小工具知识网

当前位置: 首页 > 行业知识 > 解决:requests.exceptions.ProxyError:HTTPSConnectionPool(host=‘xxx.com’,port=443):Max retries exceeded

行业知识

解决:requests.exceptions.ProxyError:HTTPSConnectionPool(host=‘xxx.com’,port=443):Max retries exceeded

2026-12-04 00:43:34 来源:互联网转载

解决:requests.exceptions.ProxyError: HTTPSConnectionPool(host=‘www.xxxx.com’, port=443): Max retries exceeded with url

文章目录

  • 解决:requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.xxxx.com', port=443): Max retries exceeded with url
    • 背景
    • 报错问题
    • 报错翻译
    • 报错位置代码
    • 报错原因
    • 解决方法
      • 方法一:增加睡眠时间,减少访问频率
      • 方法二:关闭 SSL 验证,设置verify=False
      • 方法三:释放请求链接
      • 方法四:改变重连次数,设置 `requests.adapters.DEFAULT_RETRIES = 5`
      • 方法五:SNI支持
      • 方法六:设置不使用或关闭代理
      • 方法七:降低requests和urllib3版本
      • 参考内容
        • 今天���分享就到此结束了

          背景

          在使用之前的代码时,报错:requests.exceptions.ProxyError: HTTPSConnectionPool(host=‘www.xxxx.com’, port=443): Max retries exceeded with url: / (Caused by ProxyError(‘Cannot connect to proxy.’, timeout(‘_ssl.c:1108: The handshake operation timed out’)))

          报错问题

          requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.xxxx.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', timeout('_ssl.c:1108: The handshake operation timed out')))

          报错翻译

          主要报错信息内容翻译如下所示:

          requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.xxxx.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', timeout('_ssl.c:1108: The handshake operation timed out')))

          翻译:

          requests.exceptions.proxy错误:HTTPSConnectionPool(host='ww.xxxx.com',port=443):url超过了最大重试次数:/(由ProxyError('Cannot connect to proxy.',timeout('_ssl.c:1108:握手操作超时')引起)

          报错位置代码

           

          报错原因

          经过查阅资料,发现这个错误产生的原因是 SSL 证书报错,http连接太多没有关闭导致http的连接数超过最大限制。由于http默认情况下连接是Keep-alive的,所以这就导致了服务器保持了太多连接而不能再新建连接,不然就会产生这样的错误提示。

          具体扣一分以下三种情况:

          1. ip被封

          2. 程序请求速度过快。

          3. 使用了代理

          小伙伴们按下面的解决方法即可解决!!!

          解决方法

          要解决这个错误有以下几种方法。

          方法一:增加睡眠时间,减少访问频率

          代码是:

          time.sleep()

          方法二:关闭 SSL 验证,设置verify=False

          response = requests.get(fpath_or_url,headers=headers,stream=True, verify=False)

          使用这种方案解决的时候,出现以下警告:

          InsecureRequestWarning: Unverified HTTPS requestisbeing made. Adding certificate verificationisstrongly advised.

          在语句前加上以下代码即可不会被报错:

          requests.packages.urllib3.disable_warnings() 

          方法三:释放请求链接

          requests默认是keep-alive的,可能没有释放,加参数 headers={‘Connection’:‘close’}

          # TODO ssl证书报错,参数 verify=False,同时,requests默认是keep-alive的,可能没有释放,加参数                 sess = requests.Session()sess.mount('http://', HTTPAdapter(max_retries=3)) sess.mount('https://', HTTPAdapter(max_retries=3))     sess.keep_alive = False # 关闭多余连接text = requests.get(self.target_img_url, headers=headers, stream=True, verify=False, timeout=(5,5)) #  connect 和 read 二者的 timeout,所以是一个数组with open(img_files_path, 'wb') as file:    for i in text.iter_content(1024 * 10):        file.write(i)        text.close() # 关闭,很重要,确保不要过多的链接

          方法四:改变重连次数,设置 requests.adapters.DEFAULT_RETRIES = 5

                  try:            headers = {                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',            }            # TODO 增加连接重试次数(一共4次链接)            sess = requests.Session()            sess.mount('http://', HTTPAdapter(max_retries=3))             sess.mount('https://', HTTPAdapter(max_retries=3))                 sess.keep_alive = False # 关闭多余连接                        text = requests.get(self.target_img_url, headers=headers, stream=True, verify=False, timeout=(5,5)) # connect 和 read 二者的 timeout,所以是一个数组                         with open(img_files_path, 'wb') as file:                for i in text.iter_content(1024 * 10):                    file.write(i)                                text.close() # 关闭,很重要,确保不要过多的链接         except Exception as e:            print(e,self.target_img_url)

          方法五:SNI支持

          由于python2不支持SNI,具体SNI了解转:http://blog.csdn.net/makenothing/article/details/53292335 如果想python2支持SNI,pip安装3个模块:

          1.pyOpenSSL2.ndg-httpsclient3.pyasn1

          然后在使用requests请求前添加如下代码:

          import  urllib3.contrib.pyopensslurllib3.contrib.pyopenssl.inject_into_urllib3()

          方法六:设置不使用或关闭代理

          os.environ[‘NO_PROXY’]设置为你的目标网址的域名即可。

          如果要设置多个域名:

          但是一定要是字符串型,不然会报错,源码:

          其中,这个os.environ,意思就是系统变量,[‘NO_PROXY’]的意思就是指定某个域名别用代理去处理。

          如果没有设置代理,可以看一下Windows窗口,有没有:

          fiddler,关闭fiddler之后就可以正常处理了。

          方法七:降低requests和urllib3版本

          最新的urllib3有更严格ssl的校验,所以,用以下方案解决

          pip uninstall requests urllib3  # 先卸载 pip install requests==2.27  urllib3==1.25.8 -i https://pypi.doubanio.com/simple  # 指定版本安装,不然默认会装最新版

          参考内容

          python 爬虫:https; HTTPSConnectionPool(host=‘z.jd.com’, port=443)

          今天的分享就到此结束了

          欢迎点赞评论关注三连

上一篇:孕期水肿不用愁!孕妇水肿缓解秘籍大揭秘,轻松应对水肿烦恼!

下一篇:怎么连接服务器电源设备?