在CherryPy 3.1中服务静态文件的问题
我在用CherryPy这个网页应用框架时,遇到了一些麻烦,想要提供一个静态的XML样式表来配合一些动态生成的内容。即使是我测试用的静态文本文件也无法正常显示。
我有一个静态文件叫blah.txt
,它放在我应用的根目录下的/static
文件夹里。
在我的主网站文件中(conesearch.py
包含了CherryPy的ConeSearch页面处理类):
import conesearch
cherrypy.config.update('site.config')
cherrypy.tree.mount(conesearch.ConeSearch(), "/ucac3", 'ucac3.config')
...
在site.config
文件里,我设置了以下选项:
[/]
tools.staticdir.root: conesearch.current_dir
[/static]
tools.staticdir.on: True
tools.staticdir.dir: 'static'
其中current_dir = os.path.dirname(os.path.abspath(__file__))
是在conesearch.py
文件中。
但是,我的简单测试页面(直接参考自http://www.cherrypy.org/wiki/StaticContent)却返回了404错误:
def test(self):
return """
<html>
<head>
<title>CherryPy static tutorial</title>
</head>
<body>
<a href="/static/blah.txt">Link</a>
</body>
</html>"""
test.exposed = True
它试图访问127.0.0.1:8080/static/blah.txt,我认为这个地址应该是没问题的。有没有什么想法或者建议呢?
谢谢,
西蒙
3 个回答
我有一个类似的设置。假设我想让我的网站根目录在 http://mysite.com/site,而我的网站/应用的根目录在 /path/to/www。
我在我的server.cfg文件中有以下配置设置,并且发现我的静态文件没有问题:
[global]
...
app.mount_point = '/site'
tools.staticdir.root = '/path/to/www/'
[/static]
tools.staticdir.on = True
tools.staticdir.dir = 'static'
我从静态目录中顺利提供dojo文件等内容,还有css文件。我还在使用genshi来处理模板,并且通过cherrypy.url()这个调用来确保我的其他网址设置正确。这样一来,我可以更改app.mount_point,并且我的链接也会自动更新。
我这样来提供静态文件:
config = {'/static':
{'tools.staticdir.on': True,
'tools.staticdir.dir': PATH_TO_STATIC_FILES,
}
}
cherrypy.tree.mount(MyApp(), '/', config=config)
cherrypy.config.update
这个函数只应该接收一个单层的字典(主要是 server.*
相关的设置),但是你现在传给它的是一个多层的字典,这些设置其实应该是针对每个应用的(所以应该传给 tree.mount
)。
把你 site.config
文件里的 [/]
和 [/static]
这两个部分移动到你的 ucac3.config
文件里,这样就应该能正常工作了。