在Cherrypy中运行多个类

jdb*_*org 8 python cherrypy

我正在尝试使用索引等构建一个小网站,并在/ 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)
Run Code Online (Sandbox Code Playgroud)

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

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

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

Nan*_*ali 7

更新(2017年3月13日):下面的原始答案已经过时,但我要离开它,因为它是为了反映所提出的原始问题.

官方文档现在有一个如何实现它的正确指南.


原答案:

查看默认调度程序.Dispatching的完整文档.

引用文档:

root = HelloWorld()
root.onepage = OnePage()
root.otherpage = OtherPage()
Run Code Online (Sandbox Code Playgroud)

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

此链接提供了更多详细信息,如下所示.

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()
Run Code Online (Sandbox Code Playgroud)


Mus*_*ice 6

正如fumanchu所提到的,您可以通过多次调用为您的站点创建不同的子部分cherrypy.tree.mount.下面是我正在处理的网站的简化版本,它包含一个前端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()
Run Code Online (Sandbox Code Playgroud)

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

这两个视图也有不同的配置.例如,我已经指定api使用方法调度,并且对它的访问由HTTP基本身份验证控制.

创建实例后WebService,可以根据需要调用start或stop,并负责所有清理工作.

很酷的东西.