【发布时间】:2019-01-05 01:19:00
【问题描述】:
我想将cherrypy 中显示的404 错误页面覆盖到我的自定义错误页面。挑战是读取包含我的 index.html 的目录中的所有子文件夹。这些子文件夹分别是img、css、js...
根据cherrypydocumentation我发现我可以自定义一个404错误页面覆盖这个函数并以下面的形式做一个cherrypy.config.update:
_cp_config = {
'error_page.404': os.path.join(localDir, "static/index.html")
}
我成功定制了页面,cherrypy 成功加载了我的 html。
这是我加载 html 的代码(但不是该文件夹内的子目录)。
import cherrypy
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
application = get_wsgi_application()
WEBAPP = "/app-web/"
CONF_WEBAPP = {'/':
{'tools.staticdir.on': True,
'tools.staticdir.dir': WEBAPP,
'tools.staticdir.index': 'index.html'}}
WEB_ROOT = '/webclient/'
class ServeWebApp(object):
@cherrypy.expose
def index(self):
pass
if __name__ == '__main__':
cherrypy.tree.graft(application, '/')
cherrypy.tree.mount(ServeWebApp(), '/webapp', config=CONF_WEBAPP)
cherrypy.config.update({'error_page.404': os.path.join(WEB_ROOT, "index.html")})
cherrypy.server.socket_host = "0.0.0.0"
cherrypy.server.socket_port = 8000
cherrypy.server.thread_pool = 100
cherrypy.engine.start()
cherrypy.engine.block()
我正在为一个静态网站提供功能齐全的 css 和 js 声明 CONF_WEBAPP 并在行中加载:
cherrypy.tree.mount(ServeWebApp(), '/webapp', config=CONF_WEBAPP)
在我的文件夹 WEB_ROOT 我有一个 index.html 文件和一个 一组文件夹 {css, js, fonts, img}。 我想加载该文件夹内的索引文件和所有子目录。可能吗?有没有其他方法可以获得相同的结果? 我无法使用其他工具来显示自定义页面(如 Nginx、apache)。
Another method to customize but I could not follow that way because it uses functions
【问题讨论】:
标签: python http-status-code-404 cherrypy