emptygif
언어: 한국어

모든 언어 및 프레임워크

Python standard library (http.server) Python

/pixel.gif 요청에 빈 GIF로 응답하는 Python standard library (http.server)용 Python 스니펫입니다. GIF는 시작할 때 한 번만 디코딩합니다.

Python standard library (http.server) Python

파일
server.py
실행
python3 server.py
import base64
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import urlsplit

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


class PixelHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if urlsplit(self.path).path != "/pixel.gif":
            self.send_error(404)
            return
        self.send_response(200)
        self.send_header("Content-Type", "image/gif")
        self.send_header("Content-Length", str(len(GIF)))
        self.send_header("Cache-Control", "no-store")
        self.end_headers()
        self.wfile.write(GIF)


ThreadingHTTPServer(("", 8080), PixelHandler).serve_forever()

이 스니펫은 실행해 보고, 반환된 바이트를 비교했습니다.

확인

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

공식 사이트: Python standard library (http.server)

Python의 다른 예제