Search code examples
pythoncherrypycpdispatch

_cp_dispatch not getting called in cherrypy


In the following example, I would expect to get an exception when accessing url http://127.0.0.1:8080/b/method_b. Instead, I get normal http response containing text 'method_b' in browser. No exception raised, meaning that _cp_dispatcher is not called. Am I getting something wrong about _cp_dispatch? I am using cherrypy version 3.8.0 in python 2.7.10

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())

Solution

  • Yes you're getting something wrong about _cp_dispatch it will only be called when no property/method matches the request.

    It will raise the exception if you call: http://127.0.0.1:8080/b/method_a.

    method_a doesn't exists, method_b does.