Search code examples
pythoncookiestornadostatic-files

can you set cookies with a StaticFileHandler in tornado?


Is it possible to have torando.web.StaticFileHandler set a cookie on the client's browser every time a certain static file is downloaded? Do I have to write my own handler? I can't seem to find the answer in the docs.


Solution

  • You can customize the behavior of StaticFileHandler by subclassing it and using the static_handler_class Application setting, but setting cookies in StaticFileHandler may not do what you intend. The main purpose of StaticFileHandler is to serve static files in such a way that they can be cached, including both by the users' browsers and by proxies. A proxy may serve the same static file to multiple users, Set-Cookie header and all. If you're very careful to override the caching-related headers you could make it work, but I'd recommend writing your own handler from scratch to do this instead of subclassing StaticFileHandler.