emptygif
語言: 繁體中文

所有語言和框架

Starlette Python

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