emptygif
言語: 日本語

すべての言語・フレームワーク

Raw ASGI (Uvicorn, Granian, Hypercorn) Python

/pixel.gif へのリクエストに空の GIF を返す、Raw ASGI (Uvicorn, Granian, Hypercorn) 用の Python スニペット。GIF は起動時に一度だけデコードします。

Raw ASGI (Uvicorn, Granian, Hypercorn) Python

インストール
pip install uvicorn
ファイル
app.py
実行
uvicorn app:app --port 8080
import base64

GIF = base64.b64decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
HEADERS = [
    (b"content-type", b"image/gif"),
    (b"content-length", str(len(GIF)).encode()),
    (b"cache-control", b"no-store"),
]


async def app(scope, receive, send):
    if scope["type"] != "http":
        return
    if scope["method"] == "GET" and scope["path"] == "/pixel.gif":
        await send({"type": "http.response.start", "status": 200, "headers": HEADERS})
        await send({"type": "http.response.body", "body": GIF})
    else:
        await send({"type": "http.response.start", "status": 404, "headers": []})
        await send({"type": "http.response.body", "body": b""})

このスニペットは実行し、返されたバイト列を照合しました。

確認

curl -sI http://localhost:8080/pixel.gif

公式サイト: Raw ASGI (Uvicorn, Granian, Hypercorn)

Python の他の例