ssl.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. """
  3. requests_toolbelt.ssl_adapter
  4. =============================
  5. This file contains an implementation of the SSLAdapter originally demonstrated
  6. in this blog post:
  7. https://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/
  8. """
  9. import requests
  10. from requests.adapters import HTTPAdapter
  11. from .._compat import poolmanager
  12. class SSLAdapter(HTTPAdapter):
  13. """
  14. A HTTPS Adapter for Python Requests that allows the choice of the SSL/TLS
  15. version negotiated by Requests. This can be used either to enforce the
  16. choice of high-security TLS versions (where supported), or to work around
  17. misbehaving servers that fail to correctly negotiate the default TLS
  18. version being offered.
  19. Example usage:
  20. >>> import requests
  21. >>> import ssl
  22. >>> from requests_toolbelt import SSLAdapter
  23. >>> s = requests.Session()
  24. >>> s.mount('https://', SSLAdapter(ssl.PROTOCOL_TLSv1))
  25. You can replace the chosen protocol with any that are available in the
  26. default Python SSL module. All subsequent requests that match the adapter
  27. prefix will use the chosen SSL version instead of the default.
  28. This adapter will also attempt to change the SSL/TLS version negotiated by
  29. Requests when using a proxy. However, this may not always be possible:
  30. prior to Requests v2.4.0 the adapter did not have access to the proxy setup
  31. code. In earlier versions of Requests, this adapter will not function
  32. properly when used with proxies.
  33. """
  34. __attrs__ = HTTPAdapter.__attrs__ + ['ssl_version']
  35. def __init__(self, ssl_version=None, **kwargs):
  36. self.ssl_version = ssl_version
  37. super(SSLAdapter, self).__init__(**kwargs)
  38. def init_poolmanager(self, connections, maxsize, block=False):
  39. self.poolmanager = poolmanager.PoolManager(
  40. num_pools=connections,
  41. maxsize=maxsize,
  42. block=block,
  43. ssl_version=self.ssl_version)
  44. if requests.__build__ >= 0x020400:
  45. # Earlier versions of requests either don't have this method or, worse,
  46. # don't allow passing arbitrary keyword arguments. As a result, only
  47. # conditionally define this method.
  48. def proxy_manager_for(self, *args, **kwargs):
  49. kwargs['ssl_version'] = self.ssl_version
  50. return super(SSLAdapter, self).proxy_manager_for(*args, **kwargs)