Skip to content

Uvicorn Denial

The uvicorn-denial package implements the Websocket Denial Response on Uvicorn.

Installation

pip install uvicorn-denial

Usage

You can run the following code with python main.py.

main.py
import uvicorn
import uvicorn_denial


async def app(scope, receive, send):
    await send(
        {
            "type": "websocket.http.response.start",
            "status": 200,
            "headers": [("content-length", "4")]
        }
    )
    await send({"type": "websocket.http.response.body", "body": b"haha"})


if __name__ == "__main__":
    uvicorn.run(app, ws=uvicorn_denial.WSProtocol)

Then using curl, we can make calls to our server:

curl --include \
    --no-buffer \
    --header "Connection: Upgrade" \
    --header "Upgrade: websocket" \
    --header "Host: example.com:80" \
    --header "Origin: http://example.com:80" \
    --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
    --header "Sec-WebSocket-Version: 13" \
    http://localhost:8000/

License

This project is licensed under the terms of the MIT license.