python - 具有多个 url 路径的 CherryPy MethodDispatcher

标签 python cherrypy

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/

相关文章:

python - SCIPY - 构建约束而不单独列出每个变量

python - 添加 int 和 Python 的数字

python - 如何在 SSL 下制作网站的一部分而其余部分不?

python - 我可以使用什么将脚本中的文件参数从html代码传递到cherrypy函数

python - 如何在 Beautiful Soup 4 中插入未转义的 html 片段

python - Geopandas to_crs 转换后给出错误的坐标

python - 从套接字 : Is it guaranteed to at least get x bytes? 读取

python - 在 apache 服务器上运行的 Cherrypy 应用程序出现套接字错误

python - CherryPy 是一个健壮的网络服务器吗(即,它在像 Apache 这样的巨大负载下是否可靠)?

google-app-engine - 如何在 Google App Engine 中运行 CherryPy 网络服务器