python - ssl.SSLError : tlsv1 alert protocol version

标签 python ssl https cisco

我正在为 Cisco CMX device 使用 REST API ,并尝试编写 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,但没有效果。

最佳答案

我遇到了同样的错误,谷歌把我带到了这个问题,所以这就是我所做的,希望它可以帮助遇到类似情况的其他人。

这适用于 OS X。

在终端中检查我拥有的 OpenSSL 版本:

$ python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"
>> OpenSSL 0.9.8zh 14 Jan 2016

由于我的 OpenSSL 版本太旧,因此接受的答案不起作用。

所以我不得不更新 OpenSSL。为此,我使用 Homebrew 将 Python 更新到最新版本(从 3.5 版到 3.6 版),遵循建议的一些步骤 here :

$ brew update
$ brew install openssl
$ brew install python3

然后我遇到了 PATH 和正在使用的 python 版本的问题,所以我创建了一个新的 virtualenv 以确保采用最新版本的 python:

$ virtualenv webapp --python=python3.6

问题已解决。

关于python - ssl.SSLError : tlsv1 alert protocol version,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44316292/

相关文章:

python - Django,模板上下文处理器

ssl - EKS - Envoy 动态转发代理 CA 验证错误

ssl - 此站点的证书不包含包含域名或 IP 地址的主题备用名称扩展?

ssl - 本地主机上的 Websocket 通过 https ://problems

ssl - puppet https 通信如何工作?

python - C++ 2D vector 到 2D pybind11 数组

python - 不明白为什么我在python脚本中收到此错误

ssl - 扭曲的 listenSSL 虚拟主机

php - https 跨域数据存储/检索

python - 将带有参数的 Python 装饰器合并为一个装饰器