Quart Bad Request 语法或不支持的方法

Quart Bad Request syntax or unsupported method

Python 3.7 windows

当运行 sample from quart

from quart import Quart, websocket

app = Quart(__name__)

@app.route('/')
async def hello():
    return 'hello'

@app.websocket('/ws')
async def ws():
    while True:
        await websocket.send('hello')

app.run()

当运行http://127.0.0.1:5000/ws时,得到了

Bad Request
Bad request syntax or unsupported method

您需要一个 JS 客户端来连接到 WebSocket,而不仅仅是您的浏览器。我们称它为 test-ws.html :

<!DOCTYPE html>
<html>
<body>
    <script>
    let socket = new WebSocket("ws://localhost:5000/ws");

    socket.onmessage = function(event) {
        alert(`Data received: ${event.data}`);
        socket.close();
    };
    </script>
</body>
</html>

(使用 python3 -m http.server 并转到 http://127.0.0.1:8000/test-ws.html 在浏览器中进行测试)