CherryPy MethodDispatcher with multiple url paths

耗尽温柔 提交于 2019-12-12 14:17:46

问题


Does the MethodDispatcher from CherryPy handle multiple url paths? I'm trying to do something like below, but while requests to /customers work fine, requests to /orders always return '404 Nothing matches the given URI'.

class Customers(object):
    exposed = True

    def GET(self):
        return getCustomers()

class Orders(object):
    exposed = True

    def GET(self):
        return getOrders()


class Root(object):
    pass

root = Root()
root.customers = Customers()
root.orders = Orders()

conf = {
    'global': {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 8000,
    },
    '/': {
        'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    },
}

cherrypy.quickstart(root, '/', conf)

回答1:


I think I solved it, try using:

cherrypy.tree.mount(Root())

cherrypy.tree.mount(Customers(), '/customers',
    {'/':
        {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
    }
)
cherrypy.tree.mount(Orders(), '/orders',
    {'/':
        {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
    }
)

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

It seems like in order to expose methods in Root class you have to use annotation @cherrypy.expose. Setting exposed = True probably won't work.

See my answer to my own question Combining REST dispatcher with the default one in a single CherryPy app.



来源:https://stackoverflow.com/questions/12081265/cherrypy-methoddispatcher-with-multiple-url-paths

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!