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

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

Django Python

Đoạn mã Python cho Django, 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.

Django Python

Cài đặt
pip install django
Tệp
views.py
Chạy
python manage.py runserver 8080
# pixels/views.py
import base64

from django.http import HttpResponse
from django.views.decorators.http import require_GET

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


@require_GET
def pixel(request):
    return HttpResponse(GIF, content_type="image/gif", headers={"Cache-Control": "no-store"})


# pixels/urls.py  (include("pixels.urls") from the project's urls.py)
from django.urls import path

from . import views

urlpatterns = [
    # Django URL patterns are written without the leading slash.
    path("/pixel.gif".lstrip("/"), views.pixel, name="pixel"),
]

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: Django

Thêm với Python