21

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?

mfitzp
  • 15,275
  • 7
  • 50
  • 70
grigoryvp
  • 40,413
  • 64
  • 174
  • 277

4 Answers4

36

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
  • 217,122
  • 57
  • 293
  • 297
  • 9
    With CherryPy 3.2, I had to change `cherrypy.quickstart()` to `cherrypy.engine.start()` and `cherrypy.engine.block()` before the content would be served. – technomalogical Mar 20 '12 at 19:18
  • Also with CherryPy 3.6 you have to use `cherrypy.engine.start()` and `cherrypy.engine.block()` – Nathan Romano Nov 17 '14 at 19:01
6

Here is some information on serving static content with CherryPy: http://docs.cherrypy.org/stable/progguide/files/static.html

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

# Python 3
$ python -m http.server [port]

# Python 2
$ python -m SimpleHTTPServer [port]
codeape
  • 97,830
  • 24
  • 159
  • 188
  • 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 :( – grigoryvp Apr 17 '09 at 11:22
  • 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 Apr 17 '09 at 11:30
  • 1
    The Wiki link changed to http://docs.cherrypy.org/stable/progguide/files/static.html – Cees Timmerman Nov 21 '11 at 10:31
  • The wiki link is of no help, if what you want to do is similar to the cmdline that codeape originally posted. That is, run a simple server that serves static content from a defined directory (or cwd) without having to write any code, no matter how trivial. – chb Jul 01 '12 at 20:17
0

I post this new answer because the solution of the accepted answer is outdated. 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.engine.start()
cherrypy.engine.block()

Of course this is only a summarization of what was already posted.

AzureIP
  • 338
  • 1
  • 10
0
# 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 )
grigoryvp
  • 40,413
  • 64
  • 174
  • 277
  • what if you have a file called do_contact? That file will be impossible to download? – nosklo Apr 17 '09 at 11:53
  • That was from example, seems that i was misleaded and took 'do_contract' for some kind of internal filter method to override :) – grigoryvp Apr 17 '09 at 13:22