emptygif
言語: 日本語

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

Starlette Python

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

Starlette Python

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

from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route

GIF = base64.b64decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")


async def pixel(request: Request) -> Response:
    return Response(GIF, media_type="image/gif", headers={"Cache-Control": "no-store"})


app = Starlette(routes=[Route("/pixel.gif", pixel)])

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

確認

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

公式サイト: Starlette

Python の他の例