官方文档参考地址:html
https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings安全
针对SSL Warnings,urllib3根据不一样级别的证书校验有不一样级别的警告,针对这些不一样的场景有如下几种不一样的解决办法服务器
1.不安全的请求警告ide
当在没有启用证书验证的状况下对HTTPS URL进行请求时,就会发生这种状况。解决办法以下:ui
参考官方地址:https://urllib3.readthedocs.io/en/latest/user-guide.html#sslurl
urllib3没有对HTPPS请求作校验,为了启用证书验证,须要安装一组root证书,最简单也最现实的实现方法是用certifi包,包含Mozilla的root证书包spa
安装方式:1)、pip install certifi 或者 2)、pip install urllib3[secure]日志
安装好后,当发送请求时,能够经过建立PoolManager进行证书验证code
# _*_ encoding:utf-8 _*_ import requests import urllib3 import certifi http = urllib3.PoolManager( cert_reqs = 'CERT_REQUIRED', ca_certs = certifi.where() ) #无异常 http.request('GET','https://www.baidu.com/') #有异常 http.request('GET','https://expired.badssl.com') #urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='expired.badssl.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)'),)) #报错以下 r = requests.get('https://www.baidu.com/') #requests.exceptions.SSLError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1,
u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)'),))
二、不安全的平台警告htm
这种状况发生在具备过期ssl模块的Python 2平台上。这些旧的ssl模块可能会致使一些不安全的请求,从而在它们失败的地方得到成功,并在它们应该成功的地方请求失败。遵循pyOpenSSL指南来解决这个警告。
官方文档参考地址:https://urllib3.readthedocs.io/en/latest/user-guide.html#ssl-py2
pyOpenSSL官方地址:https://pyopenssl.readthedocs.io/en/latest/
这发生在Python 2版本上,比2.7.9大。这些老版本缺乏SNI的支持。这可能致使服务器显示客户认为无效的证书。遵循pyOpenSSL指南来解决这个警告。
注:若是在确认该HTTPS请求是安全的,能够访问时,能够经过如下方法取消警告
import urllib3 urllib3.disable_warnings()#同时能够经过如下方式抓取日志logging.captureWarnings(True)