Python3请求时出现SSLError问题解决办法

Python3请求时出现SSLError问题

在Python3中,有些朋友会出现使用requests时会出现SSLError问题,类似情况如下(可以参考文末资源):

Traceback (most recent call last):
  File "./test.py", line 24, in <module>
  response = requests.get(url1, headers=headers)
  File "build/bdist.linux-x86_64/egg/requests/api.py", line 52, in get
  File "build/bdist.linux-x86_64/egg/requests/api.py", line 40, in request
  File "build/bdist.linux-x86_64/egg/requests/sessions.py", line 209, in request 
  File "build/bdist.linux-x86_64/egg/requests/models.py", line 624, in send
  File "build/bdist.linux-x86_64/egg/requests/models.py", line 300, in _build_response
  File "build/bdist.linux-x86_64/egg/requests/models.py", line 611, in send
requests.exceptions.SSLError: [Errno 1] _ssl.c:503: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

这个时候会出现最后一行显示 ‘certificate verify failed’ 也就是说出现证书验证失败的问题,而这是由于不受信任的SSL证书引起的。最快速的解决办法就是不让他被验证,让不受信任这个问题忽略掉。代码如下:

requests.get('https://example.com', verify=False)

也就是说,对于有出现这个问题的网站,取消对其的证书验证。因此也会在运行过程中出现warning,但是不妨碍最终运行结果。
请注意:这将导致证书不被验证。这会使您的应用程序面临安全风险,例如中间人攻击。
所以这个方法在运行快速/一次性应用程序/脚本的情况下是可以接受的,但不建议在制作软件过程中使用。

参考来源:
Python Requests throwing SSLError

Python requests SSL error - certificate verify failed

SSLError是一个HTTPS连接错误,通常会在进行HTTPS请求出现。它可能是由于以下原因之一: 1. 证书验证错误:当你尝试连接到一个HTTPS站点,你的请求会被发送到该站点的服务器,并验证站点证书的有效性。如果证书无效(例如已过期、与站点的域名不匹配或被吊销),那么你的请求将被拒绝,并且你将会遇到SSLError错误。 2. 代理错误:如果你使用代理服务器来连接HTTPS站点,那么代理服务器可能会被配置不正确,或者没有正确的证书,导致连接失败。 3. TLS版本不兼容:如果你的客户端和服务器之间的TLS版本不兼容,那么你也可能会遇到SSLError错误。 要解决SSLError错误,可以尝试以下方法: 1. 确保你的证书有效:检查你的证书是否过期、与站点的域名匹配,或者是否被吊销。如果证书无效,那么你需要联系站点管理员,并请求更新证书。 2. 检查代理设置:如果你使用代理服务器连接HTTPS站点,那么你需要确保代理服务器的配置正确,并且有适当的证书。 3. 更新TLS版本:如果你的客户端和服务器之间的TLS版本不兼容,那么你需要升级你的TLS版本。在Python中,你可以使用requests库来设置TLS版本,例如: ```python import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.ssl_ import DEFAULT_CIPHERS from requests.packages.urllib3.util import ssl_ s = requests.Session() s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s = requests.Session() s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.verify = True s.auth = (&#39;username&#39;, &#39;password&#39;) s.headers.update({&#39;User-Agent&#39;: &#39;Mozilla/5.0&#39;}) s.headers.update({&#39;Connection&#39;: &#39;close&#39;}) s.headers.update({&#39;Content-Type&#39;: &#39;application/json&#39;}) s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.verify = True s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.verify = True s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.verify = True s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.verify = True s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.verify = True s.verify = True s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.verify = True s.verify = True s.mount(&#39;https://&#39;, HTTPAdapter(max_retries=3)) s.keep_alive = False s.verify = True s.verify = True ``` 以上是一段使用requests库设置TLS版本的示例代码,你可以根据自己的需求进行修改和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值