Suppress InsecureRequestWarning: Unverified HTTPS request is being made in Python2.6

前端 未结 9 1093
南方客
南方客 2020-11-27 09:06

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,
           


        
9条回答
  •  误落风尘
    2020-11-27 09:36

    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)
    

提交回复
热议问题