【问题标题】:Python `hyper` library do not work on my environmentPython `hyper` 库在我的环境中不起作用
【发布时间】:2015-05-13 12:01:27
【问题描述】:

我在运行 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

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

【问题讨论】:

标签: python http ssl http2


【解决方案1】:

第一个问题是hyper不支持Python 2.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 代理吗?

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

【讨论】:

  • 好的。推特好像有问题。当我将地址更改为:conn = HTTP20Connection('www.http2bin.org:443') 时一切正常
【解决方案2】:

每个@lukasa

这是 twitter.com 中的一个已知错误。我提交的有点恰当 这是通过 Twitter 提交的错误报告。

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

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    相关资源
    最近更新 更多