|
5 | 5 | import requests
|
6 | 6 | import sys
|
7 | 7 | from requests.adapters import HTTPAdapter, DEFAULT_POOLBLOCK, DEFAULT_RETRIES, DEFAULT_POOLSIZE
|
8 |
| -from requests.packages.urllib3.util.ssl_ import create_urllib3_context |
| 8 | + |
| 9 | +try: |
| 10 | + from requests.packages.urllib3.util.ssl_ import create_urllib3_context |
| 11 | + REQUESTS_HAS_URLLIB_SSL_CONTEXT = True |
| 12 | +except ImportError: |
| 13 | + REQUESTS_HAS_URLLIB_SSL_CONTEXT = False |
| 14 | + |
9 | 15 | import ssl
|
10 | 16 |
|
11 | 17 |
|
@@ -62,10 +68,14 @@ class CbAPISessionAdapter(HTTPAdapter):
|
62 | 68 | def __init__(self, verify_hostname=True, force_tls_1_2=False, max_retries=DEFAULT_RETRIES, **pool_kwargs):
|
63 | 69 | self._cbapi_verify_hostname = verify_hostname
|
64 | 70 | self._cbapi_force_tls_1_2 = force_tls_1_2
|
| 71 | + |
| 72 | + if force_tls_1_2 and not REQUESTS_HAS_URLLIB_SSL_CONTEXT: |
| 73 | + raise ApiError("Cannot force the use of TLS1.2: Python, urllib3, and requests versions are too old.") |
| 74 | + |
65 | 75 | super(CbAPISessionAdapter, self).__init__(max_retries=max_retries, **pool_kwargs)
|
66 | 76 |
|
67 | 77 | def init_poolmanager(self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool_kwargs):
|
68 |
| - if self._cbapi_force_tls_1_2: |
| 78 | + if self._cbapi_force_tls_1_2 and REQUESTS_HAS_URLLIB_SSL_CONTEXT: |
69 | 79 | # Force the use of TLS v1.2 when talking to this Cb Response server.
|
70 | 80 | context = create_urllib3_context(ciphers=('TLSv1.2:!aNULL:!eNULL:!MD5'))
|
71 | 81 | context.options |= ssl.OP_NO_SSLv2
|
|
0 commit comments