【问题标题】:_cp_dispatch not getting called in cherrypy_cp_dispatch 没有在cherrypy中被调用
【发布时间】:2015-12-15 12:34:09
【问题描述】:

在下面的示例中,我预计在访问 url http://127.0.0.1:8080/b/method_b 时会出现异常。相反,我在浏览器中得到包含文本“method_b”的正常 http 响应。没有引发异常,这意味着不调用 _cp_dispatcher。我对 _cp_dispatch 有什么误解吗?我在 python 2.7.10 中使用cherrypy 3.8.0 版

import cherrypy

class B(object):
    def _cp_dispatch(self, vpath):
        raise Exception("Here!!")

    @cherrypy.expose
    def method_b(self):
        return "method_b"

class A(object):
    def __init__(self):
        self.b = B()

cherrypy.quickstart(A())

【问题讨论】:

    标签: python cherrypy cp dispatch


    【解决方案1】:

    是的,您对 _cp_dispatch 有误,只有在没有属性/方法与请求匹配时才会调用它。

    如果您调用:http://127.0.0.1:8080/b/method_a,它将引发异常。

    method_a 不存在,method_b 存在。

    【讨论】:

    • 确实我测试了这个。请求 /b/garbage 会触发异常。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 2018-07-28
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2016-08-23
    • 2016-10-12
    相关资源
    最近更新 更多