__init__.py 2.8 KB

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