【问题标题】:Running more than one class in Cherrypy在 Cherrypy 中运行不止一堂课
【发布时间】:2013-02-02 11:06:30
【问题描述】:

我正在尝试使用 /api 中的索引等和我想要的 api 构建一个小型站点。

例如:

class Site(object):
    @cherrypy.expose
    def index(self):
        return "Hello, World!"
    @cherrypy.expose
    def contact(self):
        return "Email us at..."
    @cherrypy.expose
    def about(self):
        return "We are..."

class Api(object):
    @cherrypy.expose
    def getSomething(self, something):
        db.get(something)
    @cherrypy.expose
    def putSomething(self, something)

所以,我希望能够访问 mysite.com/contact 和 mysite.com/Api/putSomething

如果我使用cherrypy.quickstart(Site()),我只会获取站点下的页面。

我认为有一种方法可以将类Api映射到/Api下,但是我找不到。

【问题讨论】:

    标签: python cherrypy


    【解决方案1】:

    更新(2017 年 3 月 13 日):下面的原始答案已经过时,但为了反映所提出的原始问题,我保留原样。

    The official documentation now has a proper guide on how to achieve it.


    原答案:

    查看默认调度程序。 The entire documentation for Dispatching.

    引用自文档:

    root = HelloWorld()
    root.onepage = OnePage()
    root.otherpage = OtherPage()
    

    在上面的示例中,URL http://localhost/onepage 将指向 第一个对象和 URL http://localhost/otherpage 将指向 第二个。像往常一样,此搜索是自动完成的。

    This link gives even more detail on it with a complete example shown below.

    import cherrypy
    
    class Root:
        def index(self):
            return "Hello, world!"
        index.exposed = True
    
    class Admin:
        def user(self, name=""):
            return "You asked for user '%s'" % name
        user.exposed = True
    
    class Search:
        def index(self):
            return search_page()
        index.exposed = True
    
    cherrypy.root = Root()
    cherrypy.root.admin = Admin()
    cherrypy.root.admin.search = Search()
    

    【讨论】:

    • 好答案。补充一点:您还可以根据需要多次调用 cherrypy.tree.mount 来添加处理程序。
    • 第二个链接给出 403
    【解决方案2】:

    正如fumanchu 所述,您可以通过多次调用cherrypy.tree.mount 来为您的站点创建不同的子部分。下面是我正在开发的一个网站的简化版本,它包含一个前端 Web 应用程序和一个 Restful API:

    import cherrypy
    import web
    
    class WebService(object):
    
        def __init__(self):
            app_config = {
                '/static': {
                    # enable serving up static resource files
                    'tools.staticdir.root': '/static',
                    'tools.staticdir.on': True,
                    'tools.staticdir.dir': "static",
                },
            }
    
            api_config = {
                '/': {
                    # the api uses restful method dispatching
                    'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    
                    # all api calls require that the client passes HTTP basic authentication
                    'tools.authorize.on': True,
                }
            }
    
            cherrypy.tree.mount(web.Application(), '/', config=app_config)
            cherrypy.tree.mount(web.API(), '/api', config=api_config)
    
        # a blocking call that starts the web application listening for requests
        def start(self, port=8080):
            cherrypy.config.update({'server.socket_host': '0.0.0.0', })
            cherrypy.config.update({'server.socket_port': port, })
            cherrypy.engine.start()
            cherrypy.engine.block()
    
        # stops the web application
        def stop(self):
            cherrypy.engine.stop()
    

    创建WebService 的实例会初始化两个不同的Web 应用程序。第一个是我的前端应用程序,它位于web.Application,并将在/ 上提供服务。第二个是我的restful API,它位于web.API,将在/api提供。

    这两个视图也有不同的配置。例如,我指定 api 使用方法分派,并且对它的访问由 HTTP Basic 身份验证控制。

    创建WebService 的实例后,您可以根据需要对其调用 start 或 stop,它会负责所有的清理工作。

    很酷的东西。

    【讨论】:

      猜你喜欢
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多