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

4

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?

Thigmotaxis answered 17/4, 2009 at 8:54 Comment(0)
F
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()
Furfural answered 17/4, 2009 at 11:53 Comment(2)
With CherryPy 3.2, I had to change cherrypy.quickstart() to cherrypy.engine.start() and cherrypy.engine.block() before the content would be served.Wendel
Also with CherryPy 3.6 you have to use cherrypy.engine.start() and cherrypy.engine.block()Writeoff
C
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]
Cadastre answered 17/4, 2009 at 9:6 Comment(4)
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 :(Thigmotaxis
What kind of files you are serving should be of no consequence, it should work with html files as well. See cherrypy.org/wiki/…. Another link: nabble.com/How-do-I-serve-up-static-file-pages--td20897705.htmlCadastre
The Wiki link changed to docs.cherrypy.org/stable/progguide/files/static.htmlShifflett
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.Erich
T
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 )
Thigmotaxis answered 17/4, 2009 at 11:48 Comment(2)
what if you have a file called do_contact? That file will be impossible to download?Furfural
That was from example, seems that i was misleaded and took 'do_contract' for some kind of internal filter method to override :)Thigmotaxis
W
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.

Wera answered 6/1, 2021 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.