I have a web app built with web.py that generates some HTML files and serves them up to the front end. Just by going through the web.py tutorial, it seems I need to explicitly create a class for each static HTML page I serve, but that won't work for my purposes if my web app is generating these HTML files on the fly. Ideally, I'd have one directory cache/ where I put all of my HTML files, and sending a GET request to <IP_address_of_server>:8080/cache/<any_number>.html will return the one I want. Is this possible?
How to return static HTML pages within a directory using web.py?
245 Views Asked by wheeeee At
1
Use a regular expression in your urls(), for example:
Then, your handler will get one parameter as input, which can by your file name to serve.
Calling
/foobar/15567.htmlwill result inMyHandler.GET()withfilename="15567". which allows you to find and read and return the contents of that file.Note do not do it this simply: Queries for, for example,
/foobar/../root/passwd.txtwill result in users being able to "look outside" of your cache subdirectory, possibly doing things you do not want to do -- you should check your input (the value offilenumber), for example, make sure it is all numeric, or a simple file name (no / or ..) or the resulting normalized path starts with your expected path.):