CherryPy
中的 MethodDispatcher
是否处理多个 url 路径?我正在尝试执行类似下面的操作,但是虽然对 /customers
的请求工作正常,但对 /orders
的请求总是返回“404 没有任何内容与给定的 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)
最佳答案
我想我解决了,尝试使用:
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()
似乎为了在 Root
类中公开方法,您必须使用注释 @cherrypy.expose
。设置 exposed = True
可能行不通。
查看我对自己问题的回答 Combining REST dispatcher with the default one in a single CherryPy app .
关于python - 具有多个 url 路径的 CherryPy MethodDispatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081265/