Python在使用requests模块的时候出现如下异常信息:
C:\Python\Python39\lib\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'you request domain'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
异常原因
出现这个异常通常情况下是我们关闭了SSL验证,请求时verify参数设置为了False,关于verify参数,具体你可以看:Python模块requests参数verify – SSL证书认证
解决方法
如果要关闭这个警告信息, 我们可以进行如下设置:
解决方法一:
import requests
import logging
logging.captureWarnings(True)
解决方法二:
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
详细信息
在pycharm中我们可以直接点击进入到警告信息触发的代码位置:
C:\Python\Python39\lib\site-packages\urllib3\connectionpool.py:1043:
if not conn.is_verified:
warnings.warn(
(
"Unverified HTTPS request is being made to host '%s'. "
"Adding certificate verification is strongly advised. See: "
"https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html"
"#ssl-warnings" % conn.host
),
InsecureRequestWarning,
)
大概意思就是我们在向主机发送未经验证的HTTPS请求, 强烈建议添加SSL证书验证(发出未经验证的 HTTPS 请求将会有安全风险,容易受到攻击 中间人 (MitM) 攻击。),详情可以参阅:https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings