I'm trying to make a request to an API using Python, but I'm getting an SSL error. I've searched it everywhere, but can't seem to find a fix.
This is the versions I have installed on my virtual environment:
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 26 2016, 12:10:39)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> ssl.OPENSSL_VERSION
'OpenSSL 0.9.8zg 14 July 2015'
I'm trying to use the code I found on this blog:
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import ssl
import requests
class SSLAdapter(HTTPAdapter):
'''An HTTPS Transport Adapter that uses an arbitrary SSL version.'''
def __init__(self, ssl_version=None, **kwargs):
self.ssl_version = ssl_version
super(SSLAdapter, self).__init__(**kwargs)
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(num_pools=connections,
maxsize=maxsize,
block=block,
ssl_version=self.ssl_version)
if __name__ == '__main__':
url = 'https://msesandbox.cisco.com:8081/api/location/v2/clients?sortBy=lastLocatedTime:DESC'
s = requests.Session()
s.mount("https://", SSLAdapter(ssl.PROTOCOL_SSLv2))
response = s.get(url) #line that trigger the mistake.
print (response)
This is the output:
Traceback (most recent call last):
File "/path/to/file", line 23, in <module>
response = s.get(url)
File "/Users/rafacarv/Environments/python2_7_12_cmx/venv/lib/python2.7/site-packages/requests/sessions.py", line 487, in get
return self.request('GET', url, **kwargs)
File "/Users/rafacarv/Environments/python2_7_12_cmx/venv/lib/python2.7/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/Users/rafacarv/Environments/python2_7_12_cmx/venv/lib/python2.7/site-packages/requests/sessions.py", line 585, in send
r = adapter.send(request, **kwargs)
File "/Users/rafacarv/Environments/python2_7_12_cmx/venv/lib/python2.7/site-packages/requests/adapters.py", line 477, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590)
I also tried to use the suggestion I found on this other question, which consists of using the requests_toolbelt package, but had no luck.
How can I fix this?