emptygif
Idioma: Español

Todos los lenguajes y frameworks

Python standard library (http.server) Python

Un fragmento en Python para Python standard library (http.server) que responde a las peticiones a /pixel.gif con el GIF vacío, decodificado una sola vez al arrancar.

Python standard library (http.server) Python

Archivo
server.py
Ejecutar
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()

Ejecutamos este fragmento y comparamos los bytes que devolvió.

Comprobar

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

Sitio oficial: Python standard library (http.server)

Más en Python