Skip to content

Instantly share code, notes, and snippets.

@oryband
Last active January 4, 2021 09:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oryband/4063177 to your computer and use it in GitHub Desktop.
Save oryband/4063177 to your computer and use it in GitHub Desktop.
CherryPy Static Server

Readymade CherryPy static server, for development purposes.

Use this when your Django development server just doesn't cut, and you're too lazy to install XAMPP/Apache.

I wrote this server when I started working with HTML5

So, I wrote this server, and added a URL redirect inside my Django app to serve all video content through CherryPy (instead of Django) ... and VOILA! Everything worked!

Baking instructions:

  1. Install CherryPy: pip install cherrypy or easy_install cherrypy

  2. Make sure it's installed: python -c 'import cherrypy'

  3. You can edit the listening URL/port inside the source file.

  4. The root directory is the directory where you executed the script.

Eating instructions:

Just execute the file on your shell/terminal: python cherrypy_static_server.py

#!/usr/bin/env python
"""Static file server, using Python's CherryPy. Should be used when Django's static development server just doesn't cut."""
import cherrypy
from cherrypy.lib.static import serve_file
import os.path
class Root:
@cherrypy.expose
def index(self, name):
return serve_file(os.path.join(static_dir, name))
if __name__=='__main__':
static_dir = os.path.dirname(os.path.abspath(__file__)) # Root static dir is this file's directory.
print "\nstatic_dir: %s\n" % static_dir
cherrypy.config.update( { # I prefer configuring the server here, instead of in an external file.
'server.socket_host': '0.0.0.0',
'server.socket_port': 8001,
} )
conf = {
'/': { # Root folder.
'tools.staticdir.on': True, # Enable or disable this rule.
'tools.staticdir.root': static_dir,
'tools.staticdir.dir': '',
}
}
cherrypy.quickstart(Root(), '/', config=conf) # ..and LAUNCH ! :)
@shravankale
Copy link

What's 'name' in the serve_file method?

@Swoogan
Copy link

Swoogan commented Mar 12, 2015

"4. The root directory is the directory where you executed the script."

Isn't it actually the directory where the script is located? If it was the directory where you executed the script, I think the code would have to be os.path.abspath(os.getcwd())

Also, the code can be made slightly simpler:

import os
import cherrypy

class Root(object):
    pass       

if __name__ == '__main__':
    root = os.path.dirname(os.path.abspath(__file__))  # this file's directory
    # root = os.path.dirname(os.path.abspath(os.getcwd()))  # directory script was run from

    cherrypy.config.update( {  # I prefer configuring the server here, instead of in an external file.
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 8001,
    } )
    conf = {
        '/': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': root,
            'tools.staticdir.index': 'index.html'
        }
    }
    cherrypy.quickstart(Root(), '/', conf)

@LukeMS
Copy link

LukeMS commented Dec 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment