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


解决: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: / (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.pyOpenSSL

2.ndg-httpsclient

3.pyasn1

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


import  urllib3.contrib.pyopenssl

urllib3.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)



今天的分享就到此结束了

欢迎点赞评论关注三连

在这里插入图片描述

  • 32
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ninghes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值