抑制 InsecureRequestWarning:在 Python2.6 中发出未经验证的 HTTPS 请求 [英] Suppress InsecureRequestWarning: Unverified HTTPS request is being made in Python2.6

查看:89
本文介绍了抑制 InsecureRequestWarning:在 Python2.6 中发出未经验证的 HTTPS 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我收到以下警告:

/usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py:734: InsecureRequestWarning: 正在发出未经验证的 HTTPS 请求.强烈建议添加证书验证.请参阅:https://urllib3.readthedocs.org/en/latest/security.html不安全请求警告)

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

我已按照此处

的建议进行了尝试

导入 urllib3...urllib3.disable_warnings()

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

解决方案

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

export PYTHONWARNINGS="ignore:Unverified HTTPS request"

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

导入 urllib3urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

对于 请求 <2.16.0,见下面的原始答案.

原答案

执行 urllib3.disable_warnings() 对您不起作用的原因是因为您似乎在使用请求中供应商的 urllib3 的单独实例.

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

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

导入请求从 requests.packages.urllib3.exceptions 导入 InsecureRequestWarningrequests.packages.urllib3.disable_warnings(InsecureRequestWarning)

I am writing scripts in Python2.6 with use of pyVmomi and while using one of the connection methods:

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

I get the following warning:

/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)

What's interesting is that I do not have urllib3 installed with pip (but it's there in /usr/lib/python2.6/site-packages/requests/packages/urllib3/).

I have tried as suggested here

import urllib3
...
urllib3.disable_warnings()

but that didn't change anything.

解决方案

You can disable any Python warnings via the PYTHONWARNINGS environment variable. In this case, you want:

export PYTHONWARNINGS="ignore:Unverified HTTPS request"

To disable using Python code (requests >= 2.16.0):

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

For requests < 2.16.0, see original answer below.

Original answer

The reason doing urllib3.disable_warnings() didn't work for you is because it looks like you're using a separate instance of urllib3 vendored inside of requests.

I gather this based on the path here: /usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py

To disable warnings in requests' vendored urllib3, you'll need to import that specific instance of the module:

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

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

这篇关于抑制 InsecureRequestWarning:在 Python2.6 中发出未经验证的 HTTPS 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆