python - 抑制 InsecureRequestWarning : Unverified HTTPS request is being made in Python2. 6

标签 python python-2.6 suppress-warnings urllib3 pyvmomi

我正在 Python2.6 中使用 pyVmomi 编写脚本并在使用其中一种连接方法时:

service_instance = connect.SmartConnect(host=args.ip,
                                        user=args.user,
                                        pwd=args.password)

我收到以下警告:

/usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py:734: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)

有趣的是我没有使用 pip 安装 urllib3(但它在 /usr/lib/python2.6/site-packages/requests/packages/urllib3/ 中)。 p>

我已经按照建议尝试了here

import urllib3
...
urllib3.disable_warnings()

但这并没有改变任何东西。

最佳答案

您可以通过 PYTHONWARNINGS 禁用任何 Python 警告。环境变量。在这种情况下,您需要:

export PYTHONWARNINGS="ignore:Unverified HTTPS request"

使用 Python 代码禁用 (requests >= 2.16.0):

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

对于 requests < 2.16.0 ,请参阅下面的原始答案。

原答案

这样做的原因urllib3.disable_warnings()对你不起作用是因为看起来你正在使用请求内部提供的单独的 urllib3 实例。

我根据此处的路径收集此信息:/usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py

要禁用请求的供应商 urllib3 中的警告,您需要导入该模块的特定实例:

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

关于python - 抑制 InsecureRequestWarning : Unverified HTTPS request is being made in Python2. 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27981545/

相关文章:

python - 从大量图像(*.jpg)和标签(*.mat)制作 tensorflow 数据集

python - 如何在 Tkinter 中创建一个类似 IDLE 的小型 Python Shell?

python - 使用Python解析JSON并返回所有匹配的字段

java - 使用泛型在类上实现 equals 时避免警告

g++ - gcc 警告" 'will be initialized after'

python - 检查从 GEOS C 函数返回的几何时遇到错误

python - python 2.6 中没有 spawnl 函数吗?

python - 列表理解(理想情况下)可以在 SQL 中执行相当于 'count(*)...group by...' 的操作吗?

python - 无法使用唯一的第一个键确定性地更新嵌套字典

objective-c - 忽略 Objective-C 中的 "Property type is incompatible with type inherited from"警告