Python `hyper` 库不适用于我的环境

Python `hyper` library do not work on my environment

我对 运行ning Python hyper 库添加了对 HTTP/2 协议的支持有问题。

在我的 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

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

根据@lukasa

This is a known error in twitter.com. Somewhat appropriately I filed this as a bug report via Twitter.

https://github.com/Lukasa/hyper/issues/84

第一个问题是hyper不支持Python2.7.8.

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

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

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

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