【问题标题】:Dynamic URL with CherryPY MethodDispatcher使用 CherryPY MethodDispatcher 的动态 URL
【发布时间】:2009-10-17 14:19:25
【问题描述】:

我需要配置一个支持以下 URL 方案的 RESTful 样式 URL:

  • /父/
  • /父/1
  • /parent/1/children
  • /parent/1/chidren/1

我想使用 MethodDispatcher 以便上述每个都可以具有 GET/POST/PUT/DELETE 功能。我让它适用于第一个和第二个,但不知道如何为子部分配置调度程序。我有这本书,但它几乎没有涵盖这一点,我在网上找不到任何示例。

这是我当前配置 MethodDispatcher 的方式。

root = Root()
conf = {'/' : {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}}    

cherrypy.quickstart(root, '/parent', config=conf)

任何帮助将不胜感激。

【问题讨论】:

    标签: python rest cherrypy


    【解决方案1】:

    http://tools.cherrypy.org/wiki/RestfulDispatch 可能就是您要找的。​​p>

    在 CherryPy 3.2(刚刚推出 beta 版)中,将有一个新的 _cp_dispatch 方法,您可以在对象树中使用它来做同样的事情,甚至在发生时改变遍历,有点类似于吉诃德的_q_lookup_q_resolve。见https://bitbucket.org/cherrypy/cherrypy/wiki/WhatsNewIn32#!dynamic-dispatch-by-controllers

    【讨论】:

    • 完美。这正是我需要的,但我找不到它,因为我在谷歌搜索中专注于 MethodDispatcher。谢谢。
    • 控制器动态调度链接的链接已更改。你可以在这里找到它,bitbucket.org/cherrypy/cherrypy/wiki/WhatsNewIn32
    • 感谢您指出_cp_dispatch方法!我发现这是解决我的应用程序结构中类似问题的优雅解决方案。
    【解决方案2】:
    #!/usr/bin/env python
    import cherrypy
    
    class Items(object):
        exposed = True
        def __init__(self):
            pass
    
        # this line will map the first argument after / to the 'id' parameter
        # for example, a GET request to the url:
        # http://localhost:8000/items/
        # will call GET with id=None
        # and a GET request like this one: http://localhost:8000/items/1
        # will call GET with id=1
        # you can map several arguments using:
        # @cherrypy.propargs('arg1', 'arg2', 'argn')
        # def GET(self, arg1, arg2, argn)
        @cherrypy.popargs('id')
        def GET(self, id=None):
            print "id: ", id
            if not id:
                # return all items
            else:
                # return only the item with id = id
    
        # HTML5 
        def OPTIONS(self):                                                      
            cherrypy.response.headers['Access-Control-Allow-Credentials'] = True
            cherrypy.response.headers['Access-Control-Allow-Origin'] = cherrypy.request.headers['ORIGIN']
            cherrypy.response.headers['Access-Control-Allow-Methods'] = 'GET, PUT, DELETE'                                     
            cherrypy.response.headers['Access-Control-Allow-Headers'] = cherrypy.request.headers['ACCESS-CONTROL-REQUEST-HEADERS']
    
    class Root(object):
        pass
    
    root = Root()
    root.items = Items()
    
    conf = {
        'global': {
            'server.socket_host': '0.0.0.0',
            'server.socket_port': 8000,
        },
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
        },
    }
    cherrypy.quickstart(root, '/', conf)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多