python bottle部署_如何使用python-Bottle框架创建REST API应用程序,以及如何在apache服务器上部署restapi应用程序?...

尝试使用“method=GET/POST/PUT/DELETE”

配方-api.pyimport 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/', 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/', 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/', 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/', 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)

配置.json{

"paths" : {

"xml" : "xml/"

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值