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