【问题标题】:CherryPy MethodDispatcher with multiple url paths具有多个 url 路径的 CherryPy MethodDispatcher
【发布时间】:2012-08-22 20:54:47
【问题描述】:

来自CherryPyMethodDispatcher 是否处理多个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 版本中的错误。我无法在另一台机器上重现它,所以我清理了我的环境,重新安装了所有东西,问题就消失了。

标签: python cherrypy


【解决方案1】:

我想我解决了,尝试使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2020-05-31
    • 2020-07-26
    • 2020-10-02
    相关资源
    最近更新 更多