3

python Bottleフレームワークを使ってAPI用のサンプルアプリを1つ作成したいのですが、そのアプリをapacheサーバーにもデプロイしたいので、以下のサンプルコードを使用します。

from bottle import route, run, template

@route('/hello/:name')
def index(name='World'):
    return template('<b>Hello {{name}}</b>!', name=name)

@route('/events/:id', method='GET')
def get_event(id):
    return dict(name = 'Event ' + str(id))
run(host='localhost', port=8082)

上記のコードを使用して、サンプル アプリケーションを作成する方法と、そのサンプル アプリケーションをサーバーにデプロイする方法を説明します。どうすればこれを達成できますか?

4

3 に答える 3

1

「method=GET/POST/PUT/DELETE」を使用してみてください

レシピ-api.py

import json
import os
from bottle import route, run, static_file, request

config_file = open( 'config.json' )
config_data = json.load( config_file )
pth_xml     = config_data["paths"]["xml"]

@route('/recipes/')
def recipes_list():
    paths = []
    ls = os.listdir( pth_xml )
    for entry in ls:
        if ".xml" == os.path.splitext( entry )[1]:
            paths.append( entry )
    return { "success" : True, "paths" : paths }

@route('/recipes/<name>', method='GET')
def recipe_show( name="" ):
    if "" != name:
        return static_file( name, pth_xml  )
    else:
        return { "success" : False, "error" : "show called without a filename" }

@route('/recipes/_assets/<name>', method='GET')
def recipe_show( name="" ):
    if "" != name:
        return static_file( name, pth_xml + "_assets/" )
    else:
        return { "success" : False, "error" : "show called without a filename" }

@route('/recipes/<name>', method='DELETE' )
def recipe_delete( name="" ):
    if "" != name:
        try:
            os.remove( os.path.join( pth_xml, name + ".xml" ) )
            return { "success" : True  }
        except:
            return { "success" : False  }


@route('/recipes/<name>', method='PUT')
def recipe_save( name="" ):
    xml = request.forms.get( "xml" )
    if "" != name and "" != xml:
        with open( os.path.join( pth_xml, name + ".xml" ), "w" ) as f:
            f.write( xml )
        return { "success" : True, "path" : name }
    else:
        return { "success" : False, "error" : "save called without a filename or content" }

run(host='localhost', port=8080, debug=True)

config.json

{
    "paths" : {
        "xml" : "xml/"
    }
}
于 2015-01-05T14:07:37.357 に答える
0

ここでは、WSGI を使用して apache にボトル アプリをデプロイする方法について説明します: http://bottlepy.org/docs/dev/deployment.html#apache-mod-wsgi

アプリケーションに関する限り、REST に最も準拠する必要があるため、REST とボトルについて学びます。私が使用した優れたチュートリアルは次のとおりです。 api-in-python-using-bottle-and-mongodb/

于 2012-10-27T07:59:28.363 に答える