与popagrs装饰的Cherrypy调度员。子节点路由

时间:2017-07-08 11:40:50

标签: server url-routing cherrypy dispatcher

我想让以下网址回复

  1. / book / [bookname](工作)
  2. / book / getdetails / [bookname](工作)
  3. / book / [bookname] / page / [pagenumber](工作)
  4. / book / [bookname] / page / getdetails / [pagenumber] (不工作)
  5. / book / [bookname] / page / test(working)
  6. 基本服务处理程序:

    class Service(object):
        def __init__(self):
            self.book = Book()
    
        @cherrypy.expose
        def index(self):
            return 'normal page'
    

    图书类:

    @cherrypy.popargs('book_name')
    class Book(object):
    
        def __init__(self):
            self.page = Page()
    
        @cherrypy.expose
        @cherrypy.tools.json_out()
        def index(self, book_name):
            return {"book name": book_name}
    
        @cherrypy.expose
        @cherrypy.tools.json_out()
        def getdetails(self, book_name):
            return {"details": bookname}
    

    Page Class:

    @cherrypy.popargs('page_number')
    class Page(object):
    
        @cherrypy.expose
        @cherrypy.tools.json_out()
        def index(self, book_name, page_number):
            return {"book": book_name, "no": page_number}
    
        # DOES NOT WORK AS EXPEXTED
        # @cherrypy.expose
        # @cherrypy.tools.json_out()
        # def getdetails(self, book_name, page_number):
        #     return {"book": book_name, "no": page_number}
    
        @cherrypy.expose
        @cherrypy.tools.json_out()
        def test(self, book_name):
            return {"details": "some test"}
    

    服务是根类

    if __name__ == '__main__':
        cherrypy.quickstart(Service())
    

    `

0 个答案:

没有答案