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