fingerprint.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. """Submodule containing the implementation for the FingerprintAdapter.
  3. This file contains an implementation of a Transport Adapter that validates
  4. the fingerprints of SSL certificates presented upon connection.
  5. """
  6. from requests.adapters import HTTPAdapter
  7. from .._compat import poolmanager
  8. class FingerprintAdapter(HTTPAdapter):
  9. """
  10. A HTTPS Adapter for Python Requests that verifies certificate fingerprints,
  11. instead of certificate hostnames.
  12. Example usage:
  13. .. code-block:: python
  14. import requests
  15. import ssl
  16. from requests_toolbelt.adapters.fingerprint import FingerprintAdapter
  17. twitter_fingerprint = '...'
  18. s = requests.Session()
  19. s.mount(
  20. 'https://twitter.com',
  21. FingerprintAdapter(twitter_fingerprint)
  22. )
  23. The fingerprint should be provided as a hexadecimal string, optionally
  24. containing colons.
  25. """
  26. __attrs__ = HTTPAdapter.__attrs__ + ['fingerprint']
  27. def __init__(self, fingerprint, **kwargs):
  28. self.fingerprint = fingerprint
  29. super(FingerprintAdapter, self).__init__(**kwargs)
  30. def init_poolmanager(self, connections, maxsize, block=False):
  31. self.poolmanager = poolmanager.PoolManager(
  32. num_pools=connections,
  33. maxsize=maxsize,
  34. block=block,
  35. assert_fingerprint=self.fingerprint)