|
| 1 | +""" |
| 2 | +urllib3 - Thread-safe connection pooling and re-using. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import absolute_import |
| 6 | +import warnings |
| 7 | + |
| 8 | +from .connectionpool import ( |
| 9 | + HTTPConnectionPool, |
| 10 | + HTTPSConnectionPool, |
| 11 | + connection_from_url |
| 12 | +) |
| 13 | + |
| 14 | +from . import exceptions |
| 15 | +from .filepost import encode_multipart_formdata |
| 16 | +from .poolmanager import PoolManager, ProxyManager, proxy_from_url |
| 17 | +from .response import HTTPResponse |
| 18 | +from .util.request import make_headers |
| 19 | +from .util.url import get_host |
| 20 | +from .util.timeout import Timeout |
| 21 | +from .util.retry import Retry |
| 22 | + |
| 23 | + |
| 24 | +# Set default logging handler to avoid "No handler found" warnings. |
| 25 | +import logging |
| 26 | +try: # Python 2.7+ |
| 27 | + from logging import NullHandler |
| 28 | +except ImportError: |
| 29 | + class NullHandler(logging.Handler): |
| 30 | + def emit(self, record): |
| 31 | + pass |
| 32 | + |
| 33 | +__author__ = 'Andrey Petrov (andrey.petrov@shazow.net)' |
| 34 | +__license__ = 'MIT' |
| 35 | +__version__ = '1.21.1' |
| 36 | + |
| 37 | +__all__ = ( |
| 38 | + 'HTTPConnectionPool', |
| 39 | + 'HTTPSConnectionPool', |
| 40 | + 'PoolManager', |
| 41 | + 'ProxyManager', |
| 42 | + 'HTTPResponse', |
| 43 | + 'Retry', |
| 44 | + 'Timeout', |
| 45 | + 'add_stderr_logger', |
| 46 | + 'connection_from_url', |
| 47 | + 'disable_warnings', |
| 48 | + 'encode_multipart_formdata', |
| 49 | + 'get_host', |
| 50 | + 'make_headers', |
| 51 | + 'proxy_from_url', |
| 52 | +) |
| 53 | + |
| 54 | +logging.getLogger(__name__).addHandler(NullHandler()) |
| 55 | + |
| 56 | + |
| 57 | +def add_stderr_logger(level=logging.DEBUG): |
| 58 | + """ |
| 59 | + Helper for quickly adding a StreamHandler to the logger. Useful for |
| 60 | + debugging. |
| 61 | +
|
| 62 | + Returns the handler after adding it. |
| 63 | + """ |
| 64 | + # This method needs to be in this __init__.py to get the __name__ correct |
| 65 | + # even if urllib3 is vendored within another package. |
| 66 | + logger = logging.getLogger(__name__) |
| 67 | + handler = logging.StreamHandler() |
| 68 | + handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s')) |
| 69 | + logger.addHandler(handler) |
| 70 | + logger.setLevel(level) |
| 71 | + logger.debug('Added a stderr logging handler to logger: %s', __name__) |
| 72 | + return handler |
| 73 | + |
| 74 | + |
| 75 | +# ... Clean up. |
| 76 | +del NullHandler |
| 77 | + |
| 78 | + |
| 79 | +# All warning filters *must* be appended unless you're really certain that they |
| 80 | +# shouldn't be: otherwise, it's very hard for users to use most Python |
| 81 | +# mechanisms to silence them. |
| 82 | +# SecurityWarning's always go off by default. |
| 83 | +warnings.simplefilter('always', exceptions.SecurityWarning, append=True) |
| 84 | +# SubjectAltNameWarning's should go off once per host |
| 85 | +warnings.simplefilter('default', exceptions.SubjectAltNameWarning, append=True) |
| 86 | +# InsecurePlatformWarning's don't vary between requests, so we keep it default. |
| 87 | +warnings.simplefilter('default', exceptions.InsecurePlatformWarning, |
| 88 | + append=True) |
| 89 | +# SNIMissingWarnings should go off only once. |
| 90 | +warnings.simplefilter('default', exceptions.SNIMissingWarning, append=True) |
| 91 | + |
| 92 | + |
| 93 | +def disable_warnings(category=exceptions.HTTPWarning): |
| 94 | + """ |
| 95 | + Helper for quickly disabling all urllib3 warnings. |
| 96 | + """ |
| 97 | + warnings.simplefilter('ignore', category) |
0 commit comments