__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. Python HTTP library with thread-safe connection pooling, file post support, user friendly, and more
  3. """
  4. from __future__ import absolute_import
  5. # Set default logging handler to avoid "No handler found" warnings.
  6. import logging
  7. import warnings
  8. from logging import NullHandler
  9. from . import exceptions
  10. from ._version import __version__
  11. from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
  12. from .filepost import encode_multipart_formdata
  13. from .poolmanager import PoolManager, ProxyManager, proxy_from_url
  14. from .response import HTTPResponse
  15. from .util.request import make_headers
  16. from .util.retry import Retry
  17. from .util.timeout import Timeout
  18. from .util.url import get_host
  19. # === NOTE TO REPACKAGERS AND VENDORS ===
  20. # Please delete this block, this logic is only
  21. # for urllib3 being distributed via PyPI.
  22. # See: https://github.com/urllib3/urllib3/issues/2680
  23. try:
  24. import urllib3_secure_extra # type: ignore # noqa: F401
  25. except ImportError:
  26. pass
  27. else:
  28. warnings.warn(
  29. "'urllib3[secure]' extra is deprecated and will be removed "
  30. "in a future release of urllib3 2.x. Read more in this issue: "
  31. "https://github.com/urllib3/urllib3/issues/2680",
  32. category=DeprecationWarning,
  33. stacklevel=2,
  34. )
  35. __author__ = "Andrey Petrov (andrey.petrov@shazow.net)"
  36. __license__ = "MIT"
  37. __version__ = __version__
  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. logging.getLogger(__name__).addHandler(NullHandler())
  55. def add_stderr_logger(level=logging.DEBUG):
  56. """
  57. Helper for quickly adding a StreamHandler to the logger. Useful for
  58. debugging.
  59. Returns the handler after adding it.
  60. """
  61. # This method needs to be in this __init__.py to get the __name__ correct
  62. # even if urllib3 is vendored within another package.
  63. logger = logging.getLogger(__name__)
  64. handler = logging.StreamHandler()
  65. handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s"))
  66. logger.addHandler(handler)
  67. logger.setLevel(level)
  68. logger.debug("Added a stderr logging handler to logger: %s", __name__)
  69. return handler
  70. # ... Clean up.
  71. del NullHandler
  72. # All warning filters *must* be appended unless you're really certain that they
  73. # shouldn't be: otherwise, it's very hard for users to use most Python
  74. # mechanisms to silence them.
  75. # SecurityWarning's always go off by default.
  76. warnings.simplefilter("always", exceptions.SecurityWarning, append=True)
  77. # SubjectAltNameWarning's should go off once per host
  78. warnings.simplefilter("default", exceptions.SubjectAltNameWarning, append=True)
  79. # InsecurePlatformWarning's don't vary between requests, so we keep it default.
  80. warnings.simplefilter("default", exceptions.InsecurePlatformWarning, append=True)
  81. # SNIMissingWarnings should go off only once.
  82. warnings.simplefilter("default", exceptions.SNIMissingWarning, append=True)
  83. def disable_warnings(category=exceptions.HTTPWarning):
  84. """
  85. Helper for quickly disabling all urllib3 warnings.
  86. """
  87. warnings.simplefilter("ignore", category)