emptygif
Language: English

All languages and frameworks

Tornado Python

A Python snippet for Tornado that answers requests for /pixel.gif with the empty GIF, decoded once at startup.

Tornado Python

Install
pip install tornado
File
app.py
Run
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())

We ran this snippet and compared the bytes it returned.

Check it

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

Official site: Tornado

More in Python