test_fastApi.py
987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.websockets import WebSocket
import uvicorn
# from sse_starlette.sse import SSEResponse
app = FastAPI()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"You sent: {data}")
@app.websocket("/ws1")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
data = await websocket.receive_text()
await websocket.send_text(f"Message text was: {data}")
@app.get("/")
def get():
return "你好"
# @app.get("/sse", response_class=SSEResponse)
# async def sse():
# await sse.send("Hello, world!")
# await sse.send("This is a server-sent event.")
# await sse.send("This is another server-sent event.")
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port="23456")