Python `hyper` 库在我的环境中不起作用

标签 python http ssl http2

我在运行添加了对 HTTP/2 协议(protocol)支持的 Python hyper 库时遇到问题。

在我的 Fedora 机器上,我使用 pippip3 安装它,以便将它与 Python 2.7.8 和 Python 3.4.1 一起使用。然后我复制了与 twitter 连接的测试脚本:

from hyper import HTTP20Connection

conn = HTTP20Connection('twitter.com:443')
conn.request('GET', '/')
resp = conn.getresponse()

print(resp.read())

我使用 Python 2.7.8 运行它并以错误结束:

[mn:/plib] 1 ‡ python hyper_test.py 
Traceback (most recent call last):
  File "hyper_test.py", line 4, in <module>
    conn.request('GET', '/')
  File "/usr/lib/python2.7/site-packages/hyper/http20/connection.py", line 149, in request
    self.endheaders(message_body=body, final=True, stream_id=stream_id)
  File "/usr/lib/python2.7/site-packages/hyper/http20/connection.py", line 310, in endheaders
    self.connect()
  File "/usr/lib/python2.7/site-packages/hyper/http20/connection.py", line 215, in connect
    self._recv_cb()
  File "/usr/lib/python2.7/site-packages/hyper/http20/connection.py", line 580, in _recv_cb
    self._consume_single_frame()
  File "/usr/lib/python2.7/site-packages/hyper/http20/connection.py", line 496, in _consume_single_frame
    frame, length = Frame.parse_frame_header(header)
  File "/usr/lib/python2.7/site-packages/hyper/http20/frame.py", line 52, in parse_frame_header
    frame = FRAMES[type](stream_id)
KeyError: 80

它以 Python 3.4.1 的不同错误结束:

[mn:/plib] 1 ‡ python3 hyper_test.py 
Traceback (most recent call last):
  File "hyper_test.py", line 4, in <module>
    conn.request('GET', '/')
  File "/usr/lib/python3.4/site-packages/hyper/http20/connection.py", line 149, in request
    self.endheaders(message_body=body, final=True, stream_id=stream_id)
  File "/usr/lib/python3.4/site-packages/hyper/http20/connection.py", line 310, in endheaders
    self.connect()
  File "/usr/lib/python3.4/site-packages/hyper/http20/connection.py", line 204, in connect
    sock = wrap_socket(sock, self.host)
  File "/usr/lib/python3.4/site-packages/hyper/http20/tls.py", line 44, in wrap_socket
    assert ssl_sock.selected_npn_protocol() in H2_NPN_PROTOCOLS
AssertionError

什么会导致我的环境出现此类问题?

最佳答案

第一个问题是hyper不支持Python 2.7.8导致的

这是因为标准库的 SSL 模块在 Python 2.7.8 中非常糟糕,并且缺乏对我们需要的许多功能的支持,包括 SSL 上下文、NPN 和 ALPN。 PyOpenSSL 旨在用于弥合差距,但我的向 PyOpenSSL 添加 NPN 和 ALPN 支持的拉取请求已被搁置 9 个多月未合并。

如果您想将 hyper 与 Python 2.7 结合使用,则需要使用 2.7.9 版,其标准库 SSL 模块得到了极大改进。

至于 Python 3.4,该错误是因为 Twitter 未能与我们协商 HTTP/2。这有点出乎意料,因为今天早上从英国来的我似乎工作正常。你有某种中间 MITM 代理吗?

我确实需要更清楚地说明该错误,因此我认为这是一个错误:我会将其从断言更新为带有正确错误消息的异常。

关于Python `hyper` 库在我的环境中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28990996/

相关文章:

python - cx_Oracleexecutemany 与executemanyprepared

ruby-on-rails - 测试和生产环境中的错误 ETag 值

java - 默认的 java cacerts 不起作用

php - 使用 .htaccess 和 mod_rewrite 强制 SSL/https

regex - 仅匹配管理子文件夹时的 Nginx HTTPS

python - 重用执行标志检查的方法中的代码

python - 使用 `raw` 指令时 README.rst 无法在 PyPi 上呈现

python - 安装 Python 的多个主要版本和位版本

java - http请求的url编码正文

java - Chrome 并不总是将表单数据发送到我的(自行实现的)服务器