emptygif
Idioma: Español

Todos los lenguajes y frameworks

Raw ASGI (Uvicorn, Granian, Hypercorn) Python

Un fragmento en Python para Raw ASGI (Uvicorn, Granian, Hypercorn) que responde a las peticiones a /pixel.gif con el GIF vacío, decodificado una sola vez al arrancar.

Raw ASGI (Uvicorn, Granian, Hypercorn) Python

Instalar
pip install uvicorn
Archivo
app.py
Ejecutar
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""})

Ejecutamos este fragmento y comparamos los bytes que devolvió.

Comprobar

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

Sitio oficial: Raw ASGI (Uvicorn, Granian, Hypercorn)

Más en Python