flask restful build simple interfaces, moco basis

from flask import Flask, jsonify, abort, make_response


app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False #jsonify输出中文不乱码

@app.route('/')
def hello_world():
return 'Hello World!'


tasks = [
{
'id': 1,
'title': 'Buy groceries',
'description': 'Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': 'Learn Python',
'description': '噢噢!',
'done': False
},
{

}
]
#1.路由 2.<>Url which is passed variables with strong transition, the default STR DEF mocoapi (ID):
@ app.route ( '/ mocoapi / <int: ID>', Methods = [ 'the GET'])

# If len (tasks [id] ) <0: # Note that this will not lead normal conditions should throw 404 because not satisfy this condition did not throw 404
# ABORT (404) thrown # 404
return jsonify ({ 'Task': Tasks [ID]})
# 404 custom
@ app.errorhandler (404)
DEF NOT_FOUND (error):
return make_response (jsonify ({ 'error': 'Not Found'}), '404')


IF the __name__ == '__main__ ':
app.run (Debug =' Ture ')

Guess you like

Origin www.cnblogs.com/oo-llm/p/12071168.html