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
Recursive Function Returning None
How to Assign the Value of a Variable Using Eval in Python
Python Input Never Equals an Integer
Why Can't I Repeat the 'For' Loop for CSV.Reader
Sys.Argv[1], Indexerror: List Index Out of Range
How to Get All Combinations of Length N in Python
Openpyxl 1.8.5: Reading the Result of a Formula Typed in a Cell Using Openpyxl
Region: Ioerror: [Errno 22] Invalid Mode ('W') or Filename
Reset Color Cycle in Matplotlib
Most Efficient Way to Search the Last X Lines of a File
Remove Duplicate Rows from Pandas Dataframe Where Only Some Columns Have the Same Value
Selenium - Chromedriver Executable Needs to Be in Path
Python's Sum VS. Numpy's Numpy.Sum
How to Set Selenium Webdriver from Headless Mode to Normal Mode Within the Same Session
Finding Index of Nearest Point in Numpy Arrays of X and Y Coordinates