代码如下,用python的requests发送https请求,下载文件
down_url = 'https://ip:port' # https 地址
requests.get(down_url) # 发送https请求
控制台返回报错如下:
从提示SSLCertVerificationError可以看出证书认证错误
requests.get(down_url, verify=False) # 发送https请求时,加入verify=False,忽略证书验证
给服务器上传正式证书。条件有限的情况下,不做考虑,所以不推荐该方法
问题一通过verify=False搞定后,又报错如下,提示不安全认证
# 来自官方文档
import urllib3
urllib3.disable_warnings() # 将这段代码放到调用https的代码段中,避免其他模块调用时仍报该错
打开连接,官方文档给出了解决方案
# 以下内容摘自官方文档
TLS Warnings
urllib3 will issue several different warnings based on the level of certificate verification support. These warnings indicate particular situations and can be resolved in different ways.
InsecureRequestWarning
This happens when a request is made to an HTTPS URL without certificate verification enabled. Follow the certificate verification guide to resolve this warning.
Making unverified HTTPS requests is strongly discouraged, however, if you understand the risks and wish to disable these warnings, you can use disable_warnings():
import urllib3
urllib3.disable_warnings()
Alternatively you can capture the warnings with the standard logging module:
logging.captureWarnings(True)