Suppress Insecurerequestwarning: Unverified Https Request Is Being Made in Python2.6

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

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)

How to suppress warnings about lack of cert verification in a requests HTTPS call?

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

There are two different ways to do this, both of these work.
You will have to add this to your imports

from requests.packages.urllib3.exceptions import InsecureRequestWarning

Can InsecureRequestWarnings be disabled in the requests module?

SSL, security and other warnings can be disabled with urllib3.disable_warnings() (requests uses urllib3 internally). Specifically, SSL verification warnings can be suppressed if we use disable_warnings() with an InsecureRequestWarning object.

import requests
from requests.packages import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

url = "https://www.booking.com/searchresults.en-gb.html"
with requests.Session() as s:
r = s.get(url, verify=False)
html = r.text

Note that disabling SSL Peer Verification (verify=False) may be a security issue as it can lead to MITM attacks.

InsecureRequestWarning + pytest

Restating the comment: You can remove it only by turning the SSL cert verification back on. You can, however, hide it (so the warning is still emitted, but not displayed in the warnings section):

selected tests

applying the pytest.mark.filterwarnings marker to a test, either by warning class:

@pytest.mark.filterwarnings('ignore::urllib3.exceptions.InsecureRequestWarning')
def test_example_com():
requests.get('https://www.example.com', verify=False)

or by warning message:

@pytest.mark.filterwarnings('ignore:Unverified HTTPS request is being made.*')
def test_example_com():
requests.get('https://www.example.com', verify=False)

(the difference is between single or double colons in ignore:: and ignore:).

complete test suite

Configure filterwarnings in the pytest.ini, also either by warning class:

[pytest]
filterwarnings =
ignore::urllib3.exceptions.InsecureRequestWarning

or by warning message:

[pytest]
filterwarnings =
ignore:Unverified HTTPS request is being made.*


Related Topics



Leave a reply



Submit