'Reenable urllib3 warnings

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?



Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shazow
Solution 2 Xiong Chiamiov