Eye of Hell Eye of Hell - 6 months ago 38
Python Question

How to use cherrypy as a web server for static files?

Is it any easy way to use CherryPy as an web server that will display

.html
files in some folder? All CherryPy introductory documentation states that content is dynamically generated:

import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())


Is it any easy way to use
index.html
instead of HelloWorld.index() method?

Answer

This simple code will serve files on current directory.

import os
import cherrypy

PATH = os.path.abspath(os.path.dirname(__file__))
class Root(object): pass

cherrypy.tree.mount(Root(), '/', config={
        '/': {
                'tools.staticdir.on': True,
                'tools.staticdir.dir': PATH,
                'tools.staticdir.index': 'index.html',
            },
    })

cherrypy.quickstart()
Comments