【发布时间】:2017-07-17 17:12:22
【问题描述】:
我正在尝试使用 CherryPy 提供静态文件,但我做不到。我查看了教程,但这样设置也无法正常工作。
所有这些都使用 Python 3.4
配置
config = {
'/ws': {
'tools.websocket.on': True,
'tools.websocket.handler_cls': ChatWebSocketHandler,
'tools.websocket.protocols': ['toto', 'mytest', 'hithere']
},
'/assets': {
'tools.staticdir.on': True,
'tools.staticdir.dir': constants.TEMPLATE_PATH
},
}
我正在像这样启动cherryPy
app_root = Root(args.host, args.port, args.ssl, ssl_port=args.ssl_port)
cherrypy.quickstart(app_root, '', config=config)
固定路径是
TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)),"assets/")
我也尝试过使用assets/、/assets/ 等路径来代替上述常量。
问题是它无法识别其中任何人,并且总是给出 404 错误。
【问题讨论】:
-
您的上述配置应该可以工作。我的一个应用程序中有一个几乎相同的应用程序。您需要使用您拥有的 tools.staticdir.dir 的绝对路径。唯一的区别是我使用cherrypy.tree.mount(Root(), "/", config),然后是cherrypy.engine.start(),然后是cherrypy.engine.block(),而不是快速启动。你检查权限是否正确?
-
这也可以在 Python 2.7 中运行 3 个月。