我需要向单元测试 rest api 服务器发送 HTTP/2 get 和 post 请求。 我尝试使用请求库,但它似乎不支持 HTTP/2。所以我将请求库与超级传输适配器集成在一起,如下所示 -
import requests
from hyper.contrib import HTTP20Adapter
s = requests.Session()
data={
"loop time": 3000,
"log time": 10,
"core": 2,
"default upper": "2200",
"default lower": "800",
"inactive upper": "2200",
"inactive lower": "2000",
"uncore upper": "1700",
"uncore lower": "1200",
"inactive loop time": 60,
"active between": "23:00-22:59",
"hysteresis": 3,
"rocketing": True,
"rocketing threshold": 85
}
s.verify="/path to ca-cert file"
s.mount('https://', HTTP20Adapter())
r = s.post('https://localhost:10000/global/configuration', data=data)
print(r.status_code)
print(r.url)
但它是抛出错误-
回溯(最后一次通话): 文件“”,第 1 行,位于 文件“/usr/local/lib/python3.9/site-packages/requests/sessions.py”,第 635 行,在帖子中 返回 self.request("POST", url, data=data, json=json, **kwargs) 请求中的文件“/usr/local/lib/python3.9/site-packages/requests/sessions.py”,第 587 行 resp = self.send(prep, **send_kwargs) 发送文件“/usr/local/lib/python3.9/site-packages/requests/sessions.py”,第 701 行 r = adapter.send(请求, **kwargs) 发送文件“/usr/local/lib/python3.9/site-packages/hyper/contrib.py”,第 118 行 resp = conn.get_response() 文件“/usr/local/lib/python3.9/site-packages/hyper/common/connection.py”,第 136 行,在 get_response 中 返回 self._conn.get_response(*args, **kwargs) get_response 中的文件“/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py”,第 305 行 返回 HTTP20Response(stream.getheaders(),流) 文件“/usr/local/lib/python3.9/site-packages/hyper/http20/stream.py”,第 240 行,在 getheaders 中 self._recv_cb(stream_id=self.stream_id) 文件“/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py”,第 787 行,在 _recv_cb self._single_read() 文件“/usr/local/lib/python3.9/site-packages/hyper/http20/connection.py”,第 738 行,在 _single_read 提高 ConnectionError(error_string) hyper.http20.exceptions.ConnectionError:遇到错误 FRAME_SIZE_ERROR 0x6:帧大小不正确
请建议如何解决此问题。
我试图探索增加接收器接收的帧大小,我从下面的链接得到了关注 -
https://hyper.readthedocs.io/en/latest/advanced.html#flow-control-window-managers
要实现这些对象之一,您需要子类化 BaseFlowControlManager 类并实现 increase_window_size() 方法。作为一个简单的例子,我们可以实现一个非常愚蠢的流控制管理器,它总是调整窗口大小以响应传入的数据,如下所示:
class StupidFlowControlManager(BaseFlowControlManager):
def increase_window_size(self, frame_size):
return frame_size
然后可以将类直接插入连接对象:
HTTP20Connection('http2bin.org', window_manager=StupidFlowControlManager)
但是我不知道如何在我的代码中使用这个类。请建议。