我正在尝试向服务器(虚拟托管)发出 HTTP2 请求,该服务器根据主机 header 值 (SNI) 提供 SSL 证书。
# conn = hyper.HTTP20Connection('http2.akamai.com', port=443, ssl_context=context)
# conn.request('GET', '/path', headers={'Host': 'www.mywebsite.com'})
Python 的 Hyper-h2 包不支持 SNI 或禁用证书验证! https://hyper.readthedocs.io/en/latest/advanced.html#ssl-tls-certificate-verification
禁用证书验证的一种方法是使用自定义 SSLContext,并陷入协议(protocol)断言错误
使用自定义 SSLContext 进行 HTTP2 调用的基本代码:
import ssl
import hyper
# Custom SSLCONTEXT for not verifying SSLCertificate and Hostname
# or need SSLCONTEXT for SNI support
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
context.check_hostname = False
hyper.tls._context = context
conn = hyper.HTTP20Connection('http2.akamai.com', port=443, ssl_context=context)
conn.request('GET', '/')
print conn.get_response()
错误:
Traceback (most recent call last):
File "ssl_custom.py", line 32, in <module>
conn.request('GET', '/')
File "/usr/local/lib/python2.7/site-packages/hyper/http20/connection.py", line 281, in request
self.endheaders(message_body=body, final=True, stream_id=stream_id)
File "/usr/local/lib/python2.7/site-packages/hyper/http20/connection.py", line 544, in endheaders
self.connect()
File "/usr/local/lib/python2.7/site-packages/hyper/http20/connection.py", line 373, in connect
assert proto in H2_NPN_PROTOCOLS or proto == H2C_PROTOCOL
AssertionError
编辑/更新:现在我已经学会了如何正确构建上下文 init_context()
当向支持 SNI 的服务器发出请求时问题仍然存在。
ssl_context = init_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_OPTIONAL
headers={'Host': 'www.opentable.com'}
conn = hyper.HTTP20Connection('ev-www.opentable.com.edgekey.net', port=443, ssl_context=ssl_context)
conn.request('GET', '/washington-dc-restaurants', headers=headers)
print conn.get_response()
输出:
assert proto in H2_NPN_PROTOCOLS or proto == H2C_PROTOCOL
需要一种方法来指定 SNI 或 Curl 等价物——Hyper 中的解析特性
最佳答案
通过 TLS 使用 HTTP/2 时,客户端必须与服务器协商 HTTP/2 的使用:
implementations that support HTTP/2 over TLS MUST use protocol negotiation in TLS [TLS-ALPN]
这是通过 ALPN 完成的(历史上是通过 NPN 完成的 - 因此它出现在错误消息中)。这意味着在设置上下文时,您必须在 TLS ClientHelo
消息中通告客户端支持 HTTP/2。
context.set_alpn_protocols(['h2'])
关于python - 超 HTTP2 自定义 SSLContext 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45293922/