How to serve file in webpy?

codez picture codez · Jan 20, 2011 · Viewed 11.7k times · Source

I am using webpy framefork. I want to serve static file on one of requests. Is there special method in webpy framework or I just have to read and return that file?

Answer

miku picture miku · Jan 20, 2011

If you are running the dev server (without apache):

Create a directory (also known as a folder) called static in the location of the script that runs the web.py server. Then place the static files you wish to serve in the static folder.

For example, the URL http://localhost/static/logo.png will send the image ./static/logo.png to the client.

Reference: http://webpy.org/cookbook/staticfiles


Update. If you really need to serve a static file on / you can simply use a redirect:

#!/usr/bin/env python

import web

urls = (
  '/', 'index'
)

class index:
    def GET(self):
        # redirect to the static file ...
        raise web.seeother('/static/index.html')

app = web.application(urls, globals())

if __name__ == "__main__": app.run()