Reenable urllib3 warnings
Asked Answered
K

2

9

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?

Kolomna answered 18/5, 2018 at 15:41 Comment(0)
S
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

Serdab answered 18/5, 2018 at 15:57 Comment(0)
A
4

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
Amphiaster answered 28/4, 2022 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.