Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reenable urllib3 warnings

Tags:

python

urllib3

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?

like image 215
RAbraham Avatar asked May 18 '18 15:05

RAbraham


2 Answers

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

like image 141
shazow Avatar answered Sep 17 '22 22:09

shazow


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
like image 36
Xiong Chiamiov Avatar answered Sep 17 '22 22:09

Xiong Chiamiov