emptygif
语言: 简体中文

所有语言和框架

Tornado Python

一个用于 Tornado 的 Python 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。

Tornado Python

安装
pip install tornado
文件
app.py
运行
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())

我们运行了此代码片段,并比对了它返回的字节。

验证

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

官方网站: Tornado

更多 Python 示例