__init__.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. __author__ = "Andrey Petrov (andrey.petrov@shazow.net)"
  20. __license__ = "MIT"
  21. __version__ = __version__
  22. __all__ = (
  23. "HTTPConnectionPool",
  24. "HTTPSConnectionPool",
  25. "PoolManager",
  26. "ProxyManager",
  27. "HTTPResponse",
  28. "Retry",
  29. "Timeout",
  30. "add_stderr_logger",
  31. "connection_from_url",
  32. "disable_warnings",
  33. "encode_multipart_formdata",
  34. "get_host",
  35. "make_headers",
  36. "proxy_from_url",
  37. )
  38. logging.getLogger(__name__).addHandler(NullHandler())
  39. def add_stderr_logger(level=logging.DEBUG):
  40. """
  41. Helper for quickly adding a StreamHandler to the logger. Useful for
  42. debugging.
  43. Returns the handler after adding it.
  44. """
  45. # This method needs to be in this __init__.py to get the __name__ correct
  46. # even if urllib3 is vendored within another package.
  47. logger = logging.getLogger(__name__)
  48. handler = logging.StreamHandler()
  49. handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s"))
  50. logger.addHandler(handler)
  51. logger.setLevel(level)
  52. logger.debug("Added a stderr logging handler to logger: %s", __name__)
  53. return handler
  54. # ... Clean up.
  55. del NullHandler
  56. # All warning filters *must* be appended unless you're really certain that they
  57. # shouldn't be: otherwise, it's very hard for users to use most Python
  58. # mechanisms to silence them.
  59. # SecurityWarning's always go off by default.
  60. warnings.simplefilter("always", exceptions.SecurityWarning, append=True)
  61. # SubjectAltNameWarning's should go off once per host
  62. warnings.simplefilter("default", exceptions.SubjectAltNameWarning, append=True)
  63. # InsecurePlatformWarning's don't vary between requests, so we keep it default.
  64. warnings.simplefilter("default", exceptions.InsecurePlatformWarning, append=True)
  65. # SNIMissingWarnings should go off only once.
  66. warnings.simplefilter("default", exceptions.SNIMissingWarning, append=True)
  67. def disable_warnings(category=exceptions.HTTPWarning):
  68. """
  69. Helper for quickly disabling all urllib3 warnings.
  70. """
  71. warnings.simplefilter("ignore", category)