I
I
idze2014-04-26 11:13:22
Python
idze, 2014-04-26 11:13:22

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"}),
])

But how can I give one of the five static files to a different address (for example /random/static/ )?
PS
Tornado 3.2
Python 3

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
idze, 2014-04-28
@idze

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')

K
Konstantin Dovnar, 2014-04-26
@SolidlSnake

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 question

Ask a Question

731 491 924 answers to any question