You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.7 KiB

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