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

前端 未结 9 926
南方客
南方客 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:34

    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:

    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.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-27 09:39

    For impatient, a quick way to disable python unverified HTTPS warning:

    export PYTHONWARNINGS="ignore:Unverified HTTPS request"
    
    0 讨论(0)
  • 2020-11-27 09:41

    The correct way is to read the relevant section on the provided link and 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:

    • requests ships with its own certificate bundle (but it can only be updated together with the module)
    • it will use (since requests v2.4.0) the certifi package instead if it's installed

    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 in or tampering with or stealing your data.

    Which, with today's government-backed global hacking operations like Tailored Access Operations and the Great Firewall of China that target network infrastructure, is more probable than you think.

    0 讨论(0)
  • 2020-11-27 09:42

    For Python 2.7

    Add the environment variable PYTHONWARNINGS as key and the corresponding value to be ignored like:

    os.environ['PYTHONWARNINGS']="ignore:Unverified HTTPS request"

    0 讨论(0)
  • 2020-11-27 09:48

    Per this github comment, one can disable urllib3 request warnings via requests in a 1-liner:

    requests.packages.urllib3.disable_warnings()

    This will suppress all warnings though, not just InsecureRequest (ie it will also suppress InsecurePlatform etc). In cases where we just want stuff to work, I find the conciseness handy.

    0 讨论(0)
提交回复
热议问题