google-chrome - 使用 HTTP/2 设置 Quart 服务器
问题描述
我正在尝试设置 Quart 服务器来使用 HTTP/2。我一直在尝试在以下位置浏览最少的文档:
我在哪里:
$ cat app.py
from quart import Quart, render_template, websocket
app = Quart(__name__)
@app.route("/")
async def hello():
return await render_template("index.html")
@app.route("/api")
async def json():
return {"hello": "world"}
@app.websocket("/ws")
async def ws():
while True:
await websocket.send("hello")
await websocket.send_json({"hello": "world"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001)
一些基本检查:
$ curl -I --http2 http://acme.corp:5001
HTTP/1.1 101
date: Tue, 02 Mar 2021 10:05:12 GMT
server: hypercorn-h11
connection: upgrade
upgrade: h2c
HTTP/2 200
content-type: text/html; charset=utf-8
content-length: 0
date: Tue, 02 Mar 2021 10:05:12 GMT
server: hypercorn-h2
查看输出
$ python3 app.py
* Serving Quart app 'app'
* Environment: production
* Please use an ASGI server (e.g. Hypercorn) directly in production
* Debug mode: False
* Running on http://0.0.0.0:5001 (CTRL + C to quit)
[2021-03-02 11:01:49,083] Running on http://0.0.0.0:5001 (CTRL + C to quit)
[2021-03-02 11:01:53,011] 10.221.0.114:53637 GET / 1.1 200 0 5817
[2021-03-02 11:01:53,255] 10.221.0.114:53637 GET /favicon.ico 1.1 404 103 1348
这是我从 chrome 加载 index.html 页面时看到的内容:
从 chrome 获取 http/2 我缺少什么?
解决方案
在本地,您正在将不安全的 HTTP 1.1 请求升级为不安全的HTTP 2 请求。这适用于 Quart 和 curl,但包括 chrome 在内的浏览器不支持不安全(未加密)的 HTTP/2。为了让它在 chrome 中工作,我创建了一个自签名证书,将 certfile 和 keyfile 选项传递给运行,并在访问该站点时接受 chrome 提供的警告。这里有一个例子。
推荐阅读
- javascript - 如何将 fetch 的响应解析为 JSON
- excel - 批量导出 PDF 属性
- scala - Spark 在带有 SBT 程序集 JAR 的 spark-submit 中找不到应用程序类本身 (ClassNotFoundException)
- amazon-web-services - 如何在 ECR 中重新标记图像?
- thunderbird - 筛选 addflag "$label1" 在 Thunderbird 中不显示
- c# - 针对 SQL Server 事务数据执行的实时聚合设计
- android - 安卓中的Javax
- javascript - Javascript整页预加载脚本修改
- algorithm - 解释为什么对长度为 N 的数字求和的时间复杂度是 O(logN)
- algorithm - 如何使用 LRU Cache 获取具有 k 个唯一字符的最长子字符串