8

I have a portion of my code where I knowingly make an Insecure Request. So I disable warnings with

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

After that part, how do I reenable/reset urllib3 warnings in my script?

RAbraham
  • 5,956
  • 8
  • 45
  • 80

2 Answers2

7

If you need to programmatically reset all warnings, you can do:

import warnings
warnings.resetwarnings()

This will cause all of the urllib3 warnings (and all other warnings) to revert back to the default state.

The urllib3.disable_warnings helper is a one-line wrapper around warnings.simplefilter('ignore', category).

If you'd like to apply a specific category override yourself, you can do something like:

warnings.simplefilter('default', category)

More on warning filters here: https://docs.python.org/2/library/warnings.html#available-functions

shazow
  • 17,147
  • 1
  • 34
  • 35
3

The warnings module has a documentation section on temporarily ignoring warnings. If you have one part of your code where you're making the insecure request, you can wrap that in a context:

import warnings
with warnings.catch_warnings():
    warnings.simplefilter('ignore', urllib3.exceptions.InsecureRequestWarning)
    # Run the rest of your code
Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101