首页 > 解决方案 > Quart Bad Request 语法或不支持的方法

问题描述

Windows 上的 Python 3.7

从夸脱运行样品时

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

标签: pythonquartasgi

解决方案


您需要一个 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在浏览器中进行测试)


推荐阅读