9

インデックスなどと/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をマッピングする方法があると思いますが、見つかりません。

4

2 に答える 2

8

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

のインスタンスを作成すると、WebService2つの異なるWebアプリケーションが初期化されます。1つ目は、私のフロントエンドアプリケーションです。これは、に存在しweb.Application、で提供され/ます。2つ目は私の安らかなAPIで、に住んでいてweb.API、で提供され/apiます。

2つのビューの構成も異なります。たとえば、APIがメソッドディスパッチを使用し、APIへのアクセスがHTTP基本認証によって管理されるように指定しました。

のインスタンスを作成したらWebService、必要に応じてそのインスタンスでstartまたはstopを呼び出すことができ、すべてのクリーンアップが処理されます。

かなりクールなもの。

于 2013-02-22T02:09:00.760 に答える
8

更新(2017年3月13日):以下の元の回答はかなり古くなっていますが、質問された元の質問を反映するためにそのまま残しています。

公式ドキュメントには、それを実現する方法に関する適切なガイドが含まれています。


元の回答:

デフォルトのディスパッチャを見てください。ディスパッチのドキュメント全体。

ドキュメントからの引用:

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

上記の例では、URLhttp://localhost/onepageは最初のオブジェクトをhttp://localhost/otherpage指し、URLは2番目のオブジェクトを指します。いつものように、この検索は自動的に行われます。

このリンクは、以下に示す完全な例でさらに詳細を提供します。

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()
于 2013-02-02T11:16:56.843 に答える