如何用CherryPy捕获所有异常? [英] How to catch all exceptions with CherryPy?

查看:302
本文介绍了如何用CherryPy捕获所有异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 CherryPy 来运行一个非常简单的Web服务器。它旨在处理 GET 参数,如果它们是正确的,请与他们做某事。

I use CherryPy to run a very simple web server. It is intended to process the GET parameters and, if they are correct, do something with them.

import cherrypy

class MainServer(object):
    def index(self, **params):
        # do things with correct parameters
        if 'a' in params:
            print params['a']

    index.exposed = True

cherrypy.quickstart(MainServer())

例如,

http://127.0.0.1:8080/abcde:

 404 Not Found

The path '/abcde' was not found.

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 656, in respond
    response.body = self.handler()
  File "C:\Python27\lib\site-packages\cherrypy\lib\encoding.py", line 188, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "C:\Python27\lib\site-packages\cherrypy\_cperror.py", line 386, in __call__
    raise self
NotFound: (404, "The path '/abcde' was not found.")
Powered by CherryPy 3.2.4

我正在尝试捕获此异常并显示一个空白页,因为客户端不关心它。具体来说,结果将是一个空的身体,无论是导致异常的url或查询字符串。

I am trying to catch this exception and show a blank page because the clients do not care about it. Specifically, the result would be an empty body, no matter the url or query string that resulted in an exception.

我看过关于错误处理的文档 cherrypy._cperror ,但我做了没有找到实际使用它的方法。

I had a look at documentation on error handling cherrypy._cperror, but I did not find a way to actually use it.

编辑:我放弃了使用 CherryPy 并使用 BaseHTTPServer 找到一个简单的解决方案(请参阅我的答案下面,downvoted因为它解决了问题,但没有回答问题...叹息...)

EDIT: I gave up using CherryPy and found a simple solution using BaseHTTPServer (see my answer below, downvoted because it solves the issue but does not answer the question ...sigh...)

推荐答案

CherryPy IS 捕获您的异常。这就是如何将有效的页面返回给浏览器,并发现有异常。

CherryPy IS catching your exception. That's how it returns a valid page to the browser with the caught exception.

我建议您阅读所有文档。我意识到这不是最好的文档或组织得很好,但如果你至少对它进行了滑稽,框架将会更有意义。它是一个小框架,但几乎可以从应用程序服务器中预期的一切。

I suggest you read through all the documentation. I realize it isn't the best documentation or organized well, but if you at least skim through it the framework will make more sense. It is a small framework, but does almost everything you'd expect from a application server.

import cherrypy


def show_blank_page_on_error():
    """Instead of showing something useful to developers but
    disturbing to clients we will show a blank page.

    """
    cherrypy.response.status = 500

    cherrypy.response.body = ''


class Root():
    """Root of the application"""

    _cp_config = {'request.error_response': show_blank_page_on_error}

    @cherrypy.expose
    def index(self):
        """Root url handler"""

        raise Exception 

请参阅为例上述文件提供进一步参考。

See this for the example in the documentation on the page mentioned above for further reference.

这篇关于如何用CherryPy捕获所有异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆