how to use multiple dispatchers in same cherrypy a

2019-07-27 03:31发布

问题:

I have a cherrypy application like this:

import cherrypy
from controllers import UsersController

class Root(object):


    exposed = True

    def index(self):
        return 'welcome'


if __name__ == '__main__':

    root = Root()
    root.users = UsersController()

    cherrypy.tree.mount(
        root,
        '/',
        {
            '/users' :  {'request.dispatch' : cherrypy.dispatch.MethodDispatcher()}
        }
    )

cherrypy.engine.start()
cherrypy.engine.block()

Now I wish to use MethodDispatcher() for providing REST api to /users resource and I want a default dispatcher for '/' path (where a call to root.index() is expected). Instead of writing own RoutesDispatcher() is there any way to achieve this? (e.g. using MethodDispatcher() for '/users' as shown and something like DefaultDispatcher() for '/')? BTW, the error I am getting is 'Root' object is not callable.

回答1:

Since your Root is to be served with a normal dispatcher, it should be index.exposed = True.