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 示例