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', verify=False)

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

参考来源:
Python Requests throwing SSLError

Python requests SSL error - certificate verify failed

更多推荐

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