views:

1107

answers:

3

Hello. 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?

+3  A: 

Here is some information on serving static content with CherryPy: http://www.cherrypy.org/wiki/StaticContent

BTW, here is a simple way to share the current directory over HTTP with python:

$ python -m SimpleHTTPServer [port]
codeape
I know about SimpleHTTPServer, but it is very interesting to do a same thing with cherrypy. Unfortunatly, tutorial says nothing about serving any .html file as static content - only predefined .css files :(
Eye of Hell
What kind of files you are serving should be of no consequence, it should work with html files as well. See http://www.cherrypy.org/wiki/StaticContent#Servingfilesthroughthestaticdirtool. Another link: http://www.nabble.com/How-do-I-serve-up-static-file-pages--td20897705.html
codeape
A: 
# encode: utf-8

import cherrypy
WEB_ROOT = "c:\\webserver\\root\\"

class CServer( object ) :
    @cherrypy.expose
    def do_contact(self, **params):
        pass

cherrypy.server.socket_port = 80
# INADDR_ANY: listen on all interfaces
cherrypy.server.socket_host = '0.0.0.0'
conf = { '/':
  { 'tools.staticdir.on' : True,
    'tools.staticdir.dir' : WEB_ROOT,
    'tools.staticdir.index' : 'index.html' } }
cherrypy.quickstart( CServer(), config = conf )
Eye of Hell
what if you have a file called do_contact? That file will be impossible to download?
nosklo
That was from example, seems that i was misleaded and took 'do_contract' for some kind of internal filter method to override :)
Eye of Hell
+6  A: 

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()
nosklo
Thanks a lot, that's exactly what i need!
Eye of Hell