Python+flask实现restful接口的示例详解

作者:just-do-it-zzj 时间:2023-03-23 08:34:55 

1.第一个实例:HelloWorld

1.编写python代码

from flask import Flask
app=Flask(__name__)
@app.route('/HelloWorld')
def hello_world():
   return "Hello World!"

if __name__ == "__main__":
   app.run(host='127.0.0.1',port=8085,debug=True)

2.运行代码

Python+flask实现restful接口的示例详解

3.在浏览器输入访问地址

http://127.0.0.1:8085/HelloWorld

Python+flask实现restful接口的示例详解

2.Post 方法

1.编写接口

from flask import Flask,abort,request,jsonify
import requests
tasks = []
@app.route('/add_user', methods=['POST'])
def add_user():
   if not request.json  or 'id' not in request.json or 'name' not in request.json:
       abort(400)
   task = {
       'id': request.json['id'],
       'name': request.json['name']
   }
   tasks.append(task)
   return jsonify({'result': 'success'})
if __name__ == "__main__":
   app.run(host='127.0.0.1',port=8085,debug=True)

2.运行接口

Python+flask实现restful接口的示例详解

3 使用postman测试

1)设置Headers参数

Python+flask实现restful接口的示例详解

2)设置body参数后点击“Send”

Python+flask实现restful接口的示例详解

3)返回值

Python+flask实现restful接口的示例详解

3.Get 方法

1.编写代码

from flask import Flask,abort,request,jsonify
import requests
@app.route('/get_user', methods=['GET'])
def get_user():
   if not request.args or 'id' not in request.args:
       return jsonify(tasks)
   else:
       task_id = request.args['id']
       task = filter(lambda t: t['id'] == int(task_id), tasks)
       return jsonify(task) if task else jsonify({'result': 'not found'})
if __name__ == "__main__":
   app.run(host='127.0.0.1',port=8085,debug=True)

2.运行接口

Python+flask实现restful接口的示例详解

3.使用postman测

Python+flask实现restful接口的示例详解

4.通过变量设置动态url

通过在route中添加变量<var_name>,同时把变量作为函数参数,可以实现动态url

1.编写代码

from flask import Flask,abort,request,jsonify

app=Flask(__name__)

@app.route('/getUser/<userName>')
def getUser(userName):
   return "Hello:{}!".format(userName)
if __name__ == "__main__":
   app.run(host='127.0.0.1',port=8085)

2.运行接口

Python+flask实现restful接口的示例详解

3.在浏览器输入访问地址

http://127.0.0.1:8085/getUser/zhangsan

Python+flask实现restful接口的示例详解

http://127.0.0.1:8085/getUser/lisi

Python+flask实现restful接口的示例详解

来源:https://blog.csdn.net/henku449141932/article/details/128917288

标签:Python,flask,restful接口
0
投稿

猜你喜欢

  • 防止网上重复投票的方法

    2009-06-01 12:31:00
  • Python selenium 父子、兄弟、相邻节点定位方式详解

    2023-07-03 06:19:09
  • python使用epoll实现服务端的方法

    2021-05-16 22:52:34
  • Python xlrd读取excel日期类型的2种方法

    2021-06-28 02:12:53
  • php字符串过滤strip_tags()函数用法实例分析

    2023-09-04 14:05:00
  • 教你隐藏ACCESS数据库的表名

    2008-05-09 19:45:00
  • 详解MySQL 重做日志(redo log)与回滚日志(undo logo)

    2024-01-29 13:51:01
  • 异地远程访问本地SQL Server数据库

    2024-01-18 20:00:18
  • tensorflow使用神经网络实现mnist分类

    2023-07-05 10:19:13
  • FSO无效的过程调用或参数问题

    2010-03-25 21:49:00
  • 使用python 进行区间取值的方法

    2023-12-17 01:27:56
  • 基于Python利用Pygame实现翻转图像

    2021-05-20 00:05:08
  • 深入探讨:MySQL数据库MyISAM与InnoDB存储引擎的比较

    2024-01-25 17:40:34
  • sql server建表时设置ID字段自增的简单方法

    2024-01-17 14:11:13
  • Python argparse库的基本使用步骤

    2023-12-14 08:02:29
  • js实现股票实时刷新数据案例

    2024-04-10 10:52:20
  • MySQL数据库事务与锁深入分析

    2024-01-28 19:03:12
  • python怎么判断模块安装完成

    2022-11-25 12:58:56
  • python解析html提取数据,并生成word文档实例解析

    2023-10-19 13:50:38
  • vue中英文切换实例代码

    2024-05-29 22:29:37
  • asp之家 网络编程 m.aspxhome.com