3

I want to create one sample application for api's using python Bottle framework, I want to deploy that application on apache server as well, I use following sample code,

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)

by using above code, How I can create sample application and how I can deploy that sample application on server. How can achieve this?

4

3 回答 3

1

@phihag, go through these articles by Miguel Grinberg, @miguelgrinberg ~ http://blog.miguelgrinberg.com/category/REST

Start with this article, "Designing a RESTful API with Python and Flask" ~ and if need be install Flask, go through the steps.

Then re-write the application in Bottle. Bottle is a simple framework to use and is so close to Flask I re-wrote the code reading through the example in Bottle. There is a more detailed tutorial that you can look at once you have the basics.

It's worth the effort.

于 2013-09-07T07:23:20.507 回答
1

Try to use the "method=GET/POST/PUT/DELETE"

recipes-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

Here you have explanation how to deploy bottle app in apache with the use of WSGI: http://bottlepy.org/docs/dev/deployment.html#apache-mod-wsgi

As far as application is concerned you need to best REST compliant so learn about REST and bottle, here is a good tutorial which I used: http://myadventuresincoding.wordpress.com/2011/01/02/creating-a-rest-api-in-python-using-bottle-and-mongodb/

于 2012-10-27T07:59:28.363 回答