【问题标题】:How to custom 404 error page load subfolders using cherrypy如何使用cherrypy自定义404错误页面加载子文件夹
【发布时间】: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


    【解决方案1】:

    这是我解决问题的方法。

    首先我在我的 virtualenv 中安装了 lib requests

    import requests
    from ConfigParser import RawConfigParser
    config = RawConfigParser()
    #file with my urls I would like to use in my app.
    config.read('myparams.ini')
    
    
    class PlatformAndroid(object):
        android_url = config.get('links', 'android')#static url to play store
        redir = requests.get(android_url)
        if redir.status_code < 400: #case my app is published in play store
            raise cherrypy.HTTPRedirect(android_url)
        #unpublished app
        raise cherrypy.HTTPRedirect(config.get('links','webapp')) #fallback to webapp version of my platform specific app.
    
    if __name__ == '__main__':
    
        cherrypy.tree.mount(PlatformAndroid(), '/android')
    
        cherrypy.engine.start()
        cherrypy.engine.block()
    

    myparams.ini

    [links]
    
    android : http://play.google.com/store/apps/
    ios : http://itunes.apple.com/us/app/
    webapp : http://mysite.example.com/webapp/
    

    【讨论】:

      猜你喜欢
      • 2015-03-06
      • 2011-09-17
      • 2017-05-25
      • 2020-11-12
      • 2016-04-25
      • 2013-09-16
      • 2019-11-28
      • 1970-01-01
      • 2015-08-12
      相关资源
      最近更新 更多