简体   繁体   English

在Cherrypy中运行多个类

[英]Running more than one class in Cherrypy

I'm trying to build a small site with an index etc. and an api that I want in /api. 我正在尝试使用索引等构建一个小网站,并在/ api中创建一个我想要的api。

For example: 例如:

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)

So, I'd like to be able to go to mysite.com/contact and mysite.com/Api/putSomething 所以,我希望能够访问mysite.com/contact和mysite.com/Api/putSomething

If I use cherrypy.quickstart(Site()) , I'll only get the pages under Site. 如果我使用cherrypy.quickstart(Site()) ,我只会获取Site下的页面。

I think there's a way of mapping the class Api under /Api, but I can't find it. 我认为有一种方法可以将类Api映射到/ Api下,但我找不到它。

Update (13th March, 2017): The original answer below is quite outdated but am leaving it as it is to reflect the original question that was asked. 更新(2017年3月13日):下面的原始答案已经过时,但我要离开它,因为它是为了反映所提出的原始问题。

The official documentation now has a proper guide on how to achieve it. 官方文档现在有一个如何实现它的正确指南。


Original Answer: 原答案:

Look at the default dispatcher. 查看默认调度程序。 The entire documentation for Dispatching. Dispatching的完整文档。

Quoting from the docs: 引用文档:

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

In the example above, the URL http://localhost/onepage will point at the first object and the URL http://localhost/otherpage will point at the second one. 在上面的示例中,URL http://localhost/onepage将指向第一个对象,URL http://localhost/otherpage将指向第二个对象。 As usual, this search is done automatically. 像往常一样,这种搜索是自动完成的。

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()

As fumanchu mentioned, you can create different subsections to your site with multiple calls to cherrypy.tree.mount . 正如fumanchu所提到的,您可以通过多次调用cherrypy.tree.mount为您的站点创建不同的子部分。 Below is a simplified version of a site that I'm working on that consists of both a front-end web app and a restful API: 下面是我正在处理的网站的简化版本,其中包括一个前端Web应用程序和一个安静的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()

Creating an instance of WebService initializes two different web applications. 创建WebService实例初始化两个不同的Web应用程序。 The first is my front-end application, which lives at web.Application and will be served up at / . 第一个是我的前端应用程序,它位于web.Application ,将在/ The second is my restful API, which lives at web.API and will be served up at /api . 第二个是我的restful API,它位于web.API ,将在/api

The two views have different configurations too. 这两个视图也有不同的配置。 For instance, I've specified that the api uses method dispatching, and that access to it is governed by HTTP Basic authentication. 例如,我已经指定api使用方法调度,并且对它的访问由HTTP基本身份验证控制。

Once you create an instance of WebService , you can call start or stop on it as necessary, and it takes care of all of the cleanup. 创建WebService实例后,可以根据需要调用start或stop,并负责所有清理工作。

Pretty cool stuff. 很酷的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM