emptygif
ภาษา: ไทย

ภาษาและเฟรมเวิร์กทั้งหมด

FastAPI Python

ตัวอย่างโค้ด Python สำหรับ FastAPI ที่ตอบคำขอไปยัง /pixel.gif ด้วย GIF ว่าง ซึ่งถอดรหัสเพียงครั้งเดียวตอนเริ่มทำงาน

FastAPI Python

ติดตั้ง
pip install "fastapi[standard]"
ไฟล์
main.py
รัน
fastapi run main.py --port 8080
import base64

from fastapi import FastAPI, Response

GIF = base64.b64decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

app = FastAPI()


@app.get("/pixel.gif", include_in_schema=False)
def pixel() -> Response:
    return Response(GIF, media_type="image/gif", headers={"Cache-Control": "no-store"})

เรารันตัวอย่างโค้ดนี้และเทียบไบต์ที่ได้กลับมาแล้ว

ตรวจสอบ

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

เว็บไซต์ทางการ: FastAPI

เพิ่มเติมใน Python