Answer the question
In order to leave comments, you need to log in
How to give a random static file to Tornado web?
I use Tornado for a simple home project. There was a need to give the address (address mask) one of the five static files.
An example from the documentation shows how to treat any call to some address ( /static/(.*) ) as a static request.
application = web.Application([
(r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}),
])
Answer the question
In order to leave comments, you need to log in
The simplest and most working solution turned out to be a random redirect.
class MainHandler(tornado.web.RequestHandler):
def get(self):
items = [1, 2, 3, 4, 5]
self.redirect('http://127.0.0.1:8888/static/'+str(random.choice(items))+'.png')
Here , to return an arbitrary file, it is advised to create a subclass of StaticFileHandler and operate through it.
Similarly, having a list of files and a random module, you can try to give random files.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question