emptygif
Langue: Français

Tous les langages et frameworks

Tornado Python

Un extrait Python pour Tornado qui répond aux requêtes sur /pixel.gif avec le GIF vide, décodé une seule fois au démarrage.

Tornado Python

Installer
pip install tornado
Fichier
app.py
Lancer
python app.py
import asyncio
import base64
import re

import tornado.web

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


class PixelHandler(tornado.web.RequestHandler):
    def get(self) -> None:
        self.set_header("Content-Type", "image/gif")
        self.set_header("Cache-Control", "no-store")
        self.write(GIF)


async def main() -> None:
    # Tornado routes are regular expressions, so escape the literal path.
    app = tornado.web.Application([(re.escape("/pixel.gif"), PixelHandler)])
    app.listen(8080)
    await asyncio.Event().wait()


if __name__ == "__main__":
    asyncio.run(main())

Nous avons exécuté cet extrait et comparé les octets qu’il a renvoyés.

Vérifier

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

Site officiel: Tornado

Autres extraits en Python