具有多个URL路径的CherryPy MethodDispatcher [英] CherryPy MethodDispatcher with multiple url paths

查看:102
本文介绍了具有多个URL路径的CherryPy MethodDispatcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CherryPy 中的 MethodDispatcher 是否可以处理多个URL路径?我正在尝试执行以下操作,但是在对/customers 的请求可以正常工作时,对/orders 的请求始终返回"404 Nothing matching to the给定的URI".

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)

推荐答案

我认为我已经解决了,请尝试使用:

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

似乎要在 Root 类中公开方法,您必须使用注释 @ cherrypy.expose .设置 exposed = True 可能无效.

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.

这篇关于具有多个URL路径的CherryPy MethodDispatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆