【发布时间】:2017-11-03 02:51:37
【问题描述】:
我将 REST API 用于Cisco CMX device,并尝试编写 Python 代码向 API 发出 GET 请求以获取信息。代码如下,与文件中的代码相同,只是必要的信息有所改变。
from http.client import HTTPSConnection
from base64 import b64encode
# Create HTTPS connection
c = HTTPSConnection("0.0.0.0")
# encode as Base64
# decode to ascii (python3 stores as byte string, need to pass as ascii
value for auth)
username_password = b64encode(b"admin:password").decode("ascii")
headers = {'Authorization': 'Basic {0}'.format(username_password)}
# connect and ask for resource
c.request('GET', '/api/config/v1/aaa/users', headers=headers)
# response
res = c.getresponse()
data = res.read()
但是,我不断收到以下错误:
Traceback (most recent call last):
File "/Users/finaris/PycharmProjects/test/test/test.py", line 14, in <module>
c.request('GET', '/api/config/v1/aaa/users', headers=headers)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1260, in connect
server_hostname=server_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 377, in wrap_socket
_context=self)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 752, in __init__
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 988, in do_handshake
self._sslobj.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 633, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:645)
我也尝试更新 OpenSSL,但没有效果。
【问题讨论】:
-
见github.com/CiscoDevNet/devnet-express-dna-issues/issues/16。看起来您在 Python 中使用的是旧版本的 OpenSSL。如果您使用的是带有非常旧且过时版本的 OpenSSL 的 Mac,通常会出现这种情况。
-
我在 windows 上遇到了这个问题,为 wemos 微控制器获取二进制工具。原因是谷歌给我的python 2.7.3(旧)版本下载页面我认为它是最新的......目前2.7.14是最新的,它不再有这个问题,它“自动”工作.
-
对于所有
pip installs 都收到此错误[SSL: TLSV1_ALERT_PROTOCOL_VERSION]的人来说,这是因为Python.org 网站最近弃用了TLS。见this answer。