Home:ALL Converter>Static files in cherrypy

Static files in cherrypy

Ask Time:2015-01-22T18:32:58         Author:ivan

Json Formatter

I'm using Cherrypy 3.2.3 , Python 2.7.3. Code:

from jinja2 import Environment, FileSystemLoader
import cherrypy
import os

folder=os.path.abspath(os.curdir)

class Root(object):
    @cherrypy.expose
    def index(self):
        j2_env = Environment(loader=FileSystemLoader(folder), trim_blocks=True) 
        return j2_env.get_template('index.html').render()



cherrypy.config.update({
    'environment': 'production',
    'log.screen': True,
    'server.socket_host': '127.0.0.1',
    'server.socket_port': 28807,
})

conf = {'static': {'tools.staticdir.on': True,
                   'tools.staticdir.dir' : os.path.join(folder, 'static'),
               'tools.staticdir.content_types' : {'html': 'application/octet-stream'}}}

cherrypy.config.update(conf)

print cherrypy.config
print os.path.join(folder, 'static')
cherrypy.quickstart(Root(), '/', config = conf)

Output:

{'environment': 'production', 'tools.trailing_slash.on': True, 'request.show_tracebacks': False, 'log.screen': True, 'tools.encode.on': True, 'tools.log_tracebacks.on': True, 'server.socket_port': 28807, 'checker.on': False, 'static': {'tools.staticdir.dir': '/home/ivan/cherypy/cherryblocks/static', 'tools.staticdir.content_types': {'html': 'application/octet-stream'}, 'tools.staticdir.on': True}, 'server.socket_host': '127.0.0.1', 'request.show_mismatched_params': False, 'tools.log_headers.on': False, 'engine.autoreload_on': False}

/home/ivan/cherypy/cherryblocks/static

I try to get http://127.0.0.1:28807/static/style.css:

127.0.0.1 - - [22/Jan/2015:13:24:38] "GET /static/style.css HTTP/1.1" 404 730 "" "Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0"

Error 404.

But I can open /home/ivan/cherypy/cherryblocks/static/style.css:

stat /home/ivan/cherypy/cherryblocks/static/style.css

File: «/home/ivan/cherypy/cherryblocks/static/style.css» Size: 2556

How can cherrypy serve static files?

Author:ivan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28086561/static-files-in-cherrypy
yy