cherrypy 中的静态文件
Static files in cherrypy
我正在使用 Cherrypy 3.2.3,Python 2.7.3。
代码:
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)
输出:
{'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
我尝试得到 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"
错误 404。
但是我可以打开/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
cherrypy 如何提供静态文件?
应用程序级配置字典的键应该是服务器相对路径,例如/static
。
conf = {
'/static' : {
'tools.staticdir.on' : True,
'tools.staticdir.dir' : os.path.join(folder, 'static'),
'tools.staticdir.content_types' : {'html': 'application/octet-stream'}
}
}
此外,我建议您在提问之前先在 Whosebug 上进行搜索,因为它有 quite some CherryPy static content serving questions.
我正在使用 Cherrypy 3.2.3,Python 2.7.3。 代码:
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)
输出:
{'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
我尝试得到 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"
错误 404。
但是我可以打开/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
cherrypy 如何提供静态文件?
应用程序级配置字典的键应该是服务器相对路径,例如/static
。
conf = {
'/static' : {
'tools.staticdir.on' : True,
'tools.staticdir.dir' : os.path.join(folder, 'static'),
'tools.staticdir.content_types' : {'html': 'application/octet-stream'}
}
}
此外,我建议您在提问之前先在 Whosebug 上进行搜索,因为它有 quite some CherryPy static content serving questions.