emptygif
Langue: Français

Tous les langages et frameworks

Raw ASGI (Uvicorn, Granian, Hypercorn) Python

Un extrait Python pour Raw ASGI (Uvicorn, Granian, Hypercorn) qui répond aux requêtes sur /pixel.gif avec le GIF vide, décodé une seule fois au démarrage.

Raw ASGI (Uvicorn, Granian, Hypercorn) Python

Installer
pip install uvicorn
Fichier
app.py
Lancer
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""})

Nous avons exécuté cet extrait et comparé les octets qu’il a renvoyés.

Vérifier

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

Site officiel: Raw ASGI (Uvicorn, Granian, Hypercorn)

Autres extraits en Python