簡體   English   中英

在Cherrypy中運行多個類

[英]Running more than one class in 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)

所以,我希望能夠訪問mysite.com/contact和mysite.com/Api/putSomething

如果我使用cherrypy.quickstart(Site()) ,我只會獲取Site下的頁面。

我認為有一種方法可以將類Api映射到/ Api下,但我找不到它。

更新(2017年3月13日):下面的原始答案已經過時,但我要離開它,因為它是為了反映所提出的原始問題。

官方文檔現在有一個如何實現它的正確指南。


原答案:

查看默認調度程序。 Dispatching的完整文檔。

引用文檔:

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

在上面的示例中,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()

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

創建WebService實例初始化兩個不同的Web應用程序。 第一個是我的前端應用程序,它位於web.Application ,將在/ 第二個是我的restful API,它位於web.API ,將在/api

這兩個視圖也有不同的配置。 例如,我已經指定api使用方法調度,並且對它的訪問由HTTP基本身份驗證控制。

創建WebService實例后,可以根據需要調用start或stop,並負責所有清理工作。

很酷的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM