python - 尝试使用 websockets 从 FastAPI 获取实时数据流时如何修复错误(不支持的升级请求。)?
问题描述
我如何才能获得每次迭代的响应,例如实时流源?
这是 RestAPImain.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
解决方案
Fastapi websocket 端点需要使用websocket
装饰器来定义,而不是get
装饰器:
@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/
推荐阅读
- azure - 无效资源 AADSTS500011
- python - minus between 2 dataframe based on column
- javascript - 将数据从 Angular 防护传递到组件失败
- angular - primeng p-upload 图标是错误的
- arrays - 实现按日期排序在 mongo 中添加
- c# - 在插入语句期间出现异常后无法删除 SQL 数据库
- typescript - 使用反射元数据访问类装饰器参数
- django - 我不明白 sorl-thumbnail 如何使用 db 和缓存
- google-cast - 是否可以使用 Google Cast API 在 google home/cast 设备中返回之前的投射会话
- sockets - 后端服务通信时如何区分 TCP 连接?