emptygif
Ngôn ngữ: Tiếng Việt

Tất cả ngôn ngữ và framework

Raw ASGI (Uvicorn, Granian, Hypercorn) Python

Đoạn mã Python cho Raw ASGI (Uvicorn, Granian, Hypercorn), trả lời các yêu cầu tới /pixel.gif bằng GIF rỗng, được giải mã một lần khi khởi động.

Raw ASGI (Uvicorn, Granian, Hypercorn) Python

Cài đặt
pip install uvicorn
Tệp
app.py
Chạy
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""})

Chúng tôi đã chạy đoạn mã này và so sánh các byte nó trả về.

Kiểm tra

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

Trang chính thức: Raw ASGI (Uvicorn, Granian, Hypercorn)

Thêm với Python