尝试使用 websockets 从 FastAPI 获取实时数据流时如何修复错误(不支持的升级请求。)?

How to fix an error when trying to get the live stream of data from FastAPI using websockets (Unsupported upgrade request.)?

如何获得每次迭代的响应,例如实时流提要?

这里是安息之地APImain.py:

from fastapi import FastAPI
from fastapi import Request
from fastapi import WebSocket
import asyncio

app = FastAPI()


@app.get("/ws_res")
async def websoc(websocket: WebSocket):
    await websocket.accept()
    for i in range(100000):
        i = "John"
        await asyncio.sleep(0.01)
        await websocket.send_json({"msg": i})
    await websocket.close()

现在,我正在尝试从 python 代码中获取响应,但我收到一条错误消息 Unsupported upgrade request.

这是 API 端的输出:

INFO:     Started server process [67680]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
WARNING:  Unsupported upgrade request.

这是我用来访问 API (test.py) 的 python 代码:

import asyncio
import websockets

async def hello():
    uri = "ws://127.0.0.1:8000/ws_res"
    async with websockets.connect(uri) as websocket:
        greeting = await websocket.recv()
        print(f"< {greeting['msg']}")


asyncio.get_event_loop().run_until_complete(hello())

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    asyncio.get_event_loop().run_until_complete(hello())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "test.py", line 7, in hello
    async with websockets.connect(uri) as websocket:
  File "/home/test/env/lib/python3.8/site-packages/websockets/client.py", line 517, in __aenter__
    return await self
  File "/home/test/env/lib/python3.8/site-packages/websockets/client.py", line 542, in __await_impl__
    await protocol.handshake(
  File "/home/test/env/lib/python3.8/site-packages/websockets/client.py", line 296, in handshake
    raise InvalidStatusCode(status_code)
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 400

需要使用 websocket 装饰器而不是 get 装饰器来定义 Fastapi websocket 端点:

@app.websocket("/ws_res")
async def websoc(websocket: WebSocket):
    await websocket.accept()
    for i in range(100000):
        i = "John"
        await asyncio.sleep(0.01)
        await websocket.send_json({"msg": i})
    await websocket.close()

有关完整详细信息,请参阅文档:https://fastapi.tiangolo.com/advanced/websockets/