码迷,mamicode.com
首页 > 编程语言 > 详细

python flask 接口

时间:2019-01-15 15:47:52      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:def   splay   main   接口   and   roc   image   lam   false   

 

例子1

from flask import Flask, jsonify

app = Flask(__name__)

tasks = [
    {
        id: 1,
        title: uBuy groceries,
        description: uMilk, Cheese, Pizza, Fruit, Tylenol, 
        done: False
    },
    {
        id: 2,
        title: uLearn Python,
        description: uNeed to find a good Python tutorial on the web, 
        done: False
    }
]

@app.route(/todo/api/v1.0/tasks, methods=[GET])
def get_tasks():
    return jsonify({tasks: tasks})

if __name__ == __main__:
    app.run(debug=True)

运行

python test.py

打开浏览器,访问:http://localhost:5000/todo/api/v1.0/tasks

技术分享图片

 

例子2

from flask import Flask, jsonify
from flask import abort
app = Flask(__name__)

tasks = [
    {
        id: 1,
        title: uBuy groceries,
        description: uMilk, Cheese, Pizza, Fruit, Tylenol, 
        done: False
    },
    {
        id: 2,
        title: uLearn Python,
        description: uNeed to find a good Python tutorial on the web, 
        done: False
    }
]

@app.route(/todo/api/v1.0/tasks, methods=[GET])
def get_tasks():
    return jsonify({tasks: tasks})


@app.route(/todo/api/v1.0/tasks/<int:task_id>, methods=[GET])
def get_task(task_id):
    task = list(filter(lambda t: t[id] == task_id, tasks))
    if len(task) == 0:
        abort(404)
    return jsonify({task: task[0]})
if __name__ == __main__:
    app.run(debug=True)

技术分享图片

例子3

from flask import Flask, jsonify
from flask import abort
from flask import make_response
app = Flask(__name__)

tasks = [
    {
        id: 1,
        title: uBuy groceries,
        description: uMilk, Cheese, Pizza, Fruit, Tylenol, 
        done: False
    },
    {
        id: 2,
        title: uLearn Python,
        description: uNeed to find a good Python tutorial on the web, 
        done: False
    }
]

@app.route(/todo/api/v1.0/tasks, methods=[GET])
def get_tasks():
    return jsonify({tasks: tasks})


@app.route(/todo/api/v1.0/tasks/<int:task_id>, methods=[GET])
def get_task(task_id):
    task = list(filter(lambda t: t[id] == task_id, tasks))
    if len(task) == 0:
        abort(404)
    return jsonify({task: task[0]})


@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify({error: Not found}), 404)
if __name__ == __main__:
    app.run(debug=True)

技术分享图片

 

 

python flask 接口

标签:def   splay   main   接口   and   roc   image   lam   false   

原文地址:https://www.cnblogs.com/sea-stream/p/10271769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!