简体   繁体   English

Python请求中的SSL错误

[英]SSL Error On Python Request

I'm trying to make a request to an API using Python, but I'm getting an SSL error . 我正在尝试使用Python向API发出请求,但是我收到了SSL错误 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. 我也尝试使用我在另一个问题上找到的建议,其中包括使用requests_toolbelt包,但没有运气。

How can I fix this? 我怎样才能解决这个问题?

msesandbox.cisco.com:8081 msesandbox.cisco.com:8081

This server supports only TLS 1.2, ie no TLS 1.0 or worse. 此服务器仅支持TLS 1.2,即没有TLS 1.0或更糟。

'OpenSSL 0.9.8zg 14 July 2015' 'OpenSSL 0.9.8zg 2015年7月14日'

This OpenSSL version does not support TLS 1.2 yet. 此OpenSSL版本尚不支持TLS 1.2。 You need at least OpenSSL 1.0.1 for this. 你至少需要OpenSSL 1.0.1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM