抑制 InsecureRequestWarning:在 Python2.6 中发出未经验证的 HTTPS 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27981545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Suppress InsecureRequestWarning: Unverified HTTPS request is being made in Python2.6
提问by Patryk
I am writing scripts in Python2.6 with use of pyVmomiand while using one of the connection methods:
我正在使用pyVmomi并使用其中一种连接方法在 Python2.6 中编写脚本:
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/).
有趣的是,我没有使用 pip 安装 urllib3(但它在/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.
但这并没有改变任何事情。
采纳答案by shazow
You can disable any Python warnings via the PYTHONWARNINGSenvironment variable. In this case, you want:
您可以通过PYTHONWARNINGS环境变量禁用任何 Python 警告。在这种情况下,您需要:
export PYTHONWARNINGS="ignore:Unverified HTTPS request"
To disable using Python code (requests >= 2.16.0):
要禁用使用 Python 代码 ( requests >= 2.16.0):
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
For requests < 2.16.0, see original answer below.
对于requests < 2.16.0,请参阅下面的原始答案。
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.
这样urllib3.disable_warnings()做对您不起作用的原因是因为看起来您正在使用请求内部供应的 urllib3 的单独实例。
I gather this based on the path here: /usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py
我根据此处的路径收集此信息: /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:
要在请求的供应商 urllib3 中禁用警告,您需要导入该模块的特定实例:
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
回答by ivan_pozdeev
The correct way is to read the relevant section on the provided linkand do as it says. The way specific for requests(which bundles with its own copy of urllib3), as per CA Certificates — Advanced Usage — Requests 2.8.1 documentation:
正确的方法是阅读提供的链接上的相关部分并按照说明进行操作。根据CA 证书 — 高级用法 — 请求 2.8.1 文档,特定于requests(与其自己的 副本捆绑在一起urllib3)的方式:
requestsships with its own certificate bundle (but it can only be updated together with the module)- it will use (since
requestsv2.4.0) thecertifipackageinstead if it's installed
requests附带自己的证书包(但它只能与模块一起更新)- 如果已安装,它将使用(因为
requestsv2.4.0)该certifi包
The HTTPS certificate verification security measure isn't something to be discarded light-heartedly. The Man-in-the-middle attack that it prevents safeguards you from a third party e.g. sipping a virus inor tampering with or stealingyour data.
HTTPS 证书验证安全措施不是可以轻易丢弃的。它可以防止中间人攻击,保护您免受第三方的侵害,例如吸入病毒或篡改或窃取您的数据。
Which, with today's government-backed global hacking operations like Tailored Access Operationsand the Great Firewall of Chinathat target network infrastructure, is more probable than you think.
回答by Rajive Pai
I had a similar issue with PyVmomi Client. With Python Version 2.7.9, I have solved this issue with the following line of code:
我在 PyVmomi Client 上遇到了类似的问题。在 Python 2.7.9 版本中,我使用以下代码行解决了这个问题:
default_sslContext = ssl._create_unverified_context()
self.client = \
Client(<vcenterip>, username=<username>, password=<passwd>,
sslContext=default_sslContext )
Note that, for this to work, you need Python 2.7.9 atleast.
请注意,要使其正常工作,您至少需要 Python 2.7.9。
回答by Nayana Adassuriya
This is the answer in 2017. urllib3not a part of requestsanymore
这是在2017年的答案urllib3不是的一部分requests了
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
回答by hamx0r
Per this githubcomment, one can disable urllib3request warnings via requestsin a 1-liner:
根据此 github评论,可以urllib3通过requests1-liner禁用请求警告:
requests.packages.urllib3.disable_warnings()
requests.packages.urllib3.disable_warnings()
This will suppress allwarnings though, not just InsecureRequest(ie it will also suppress InsecurePlatformetc). In cases where we just want stuff to work, I find the conciseness handy.
这将抑制所有警告,而不仅仅是InsecureRequest(即它也会抑制InsecurePlatform等)。在我们只想让东西工作的情况下,我发现简洁很方便。
回答by SpazaM
Why not using pyvmomi original functionSmartConnectNoSSL.
They addedthis function on June 14, 2016and named it ConnectNoSSL, one day afterthey changed the name to SmartConnectNoSSL, use that instead of by passing the warning with unnecessary lines of code in your project?
为什么不使用 pyvmomi原始功能SmartConnectNoSSL。他们添加了这个函数June 14, 2016并将其命名为ConnectNoSSL,在他们将名称更改为一天后SmartConnectNoSSL,使用它而不是通过在您的项目中传递带有不必要的代码行的警告?
Provides a standard method for connecting to a specified server without SSL verification. Useful when connecting to servers with self-signed certificates or when you wish to ignore SSL altogether
提供一种无需 SSL 验证即可连接到指定服务器的标准方法。在使用自签名证书连接到服务器或希望完全忽略 SSL 时很有用
service_instance = connect.SmartConnectNoSSL(host=args.ip,
user=args.user,
pwd=args.password)
回答by Wenbing Li
For impatient, a quick way to disable python unverified HTTPS warning:
对于不耐烦的人,一种禁用python未经验证的HTTPS警告的快速方法:
export PYTHONWARNINGS="ignore:Unverified HTTPS request"
回答by Imran Al Noor
For Python 2.7
对于Python 2.7
Add the environment variable PYTHONWARNINGS as key and the corresponding value to be ignored like:
添加环境变量 PYTHONWARNINGS 作为键和要忽略的相应值,例如:
os.environ['PYTHONWARNINGS']="ignore:Unverified HTTPS request"
os.environ['PYTHONWARNINGS']="ignore:Unverified HTTPS request"
回答by Yigal
The accepted answer doesn't work if some package vendors it's own copy of urllib3, in which case this will still work:
如果某些软件包供应商拥有自己的 urllib3 副本,则接受的答案不起作用,在这种情况下,这仍然有效:
import warnings
warnings.filterwarnings('ignore', message='Unverified HTTPS request')

