11

I would like to disable the warning about a lack of certificate verification in a HTTPS call using requests.

The question has been asked in the past, leading to answers about disabling a relevant request logging or the urllib3 SSL warning.

This used to work in the past (I remember successfully silencing the warnings) but seems not to work anymore?

I put together the two solutions which worked so far:

Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
>>> import requests
>>> import requests.packages
>>> import urllib3
>>> urllib3.disable_warnings()
>>> requests.packages.urllib3.disable_warnings()
>>> requests.get('https://www.google.com', verify=False)
/usr/lib/python3/dist-packages/urllib3/connectionpool.py:845: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
<Response [200]>

Is there another (current) solution to silence these warnings?

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • Can you verify that `import urllib3`, then `urllib3.__file__` produces `/usr/lib/python3/dist-packages/urllib3/__init__.py`? – Martijn Pieters Feb 13 '18 at 12:49
  • The `.disable_warnings()` call only runs `warnings.simplefilter('ignore', urllib3.exceptions.HTTPWarning)` (and `InsecureRequestWarning` is an indirect subclass of `HTTPWarning`, so is also ignored). – Martijn Pieters Feb 13 '18 at 12:50
  • @MartijnPieters: yes, `urllib3.__file__` produces `/usr/lib/python3/dist-packages/urllib3/__init__.py` – WoJ Feb 13 '18 at 12:52
  • I can't reproduce the issue then. Can you show that `import warnings; [(w[0], w[2]) for w in warnings.filters]` consists of? – Martijn Pieters Feb 13 '18 at 13:10
  • (note that `requests.packages.urllib3` is just an alias for `urllib3` now; you can see this when you echo `requests.packages.urllib3.__file__` in your interpreter, it'll produce `/usr/lib/python3/dist-packages/urllib3/__init__.py`) – Martijn Pieters Feb 13 '18 at 13:18

3 Answers3

23
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
Arnav Chawla
  • 444
  • 3
  • 15
  • 2
    You can just do it all in one line, without the additional `import`: ```requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)``` – bitinerant Sep 11 '19 at 08:43
9

Instead of using
requests.packages.urllib3.disable_warnings()

Use
requests.urllib3.disable_warnings()

fyqc
  • 121
  • 1
  • 5
1

This works for me:

import urllib3
urllib3.disable_warnings()
Bastet
  • 36
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 03 '23 at 09:58