问题
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