emptygif
語言: 繁體中文

所有語言和框架

Raw ASGI (Uvicorn, Granian, Hypercorn) Python

適用於 Raw ASGI (Uvicorn, Granian, Hypercorn) 的 Python 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,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 範例