Python编程在flask中模拟进行Restful的CRUD操作
作者:liumiaocn 时间:2021-03-11 18:44:39
这篇文章中我们将通过对HelloWorld的message进行操作,介绍一下如何使用flask进行Restful的CRUD。
概要信息
事前准备:flask
liumiaocn:flask liumiao$ which flask
/usr/local/bin/flask
liumiaocn:flask liumiao$ flask --version
Flask 1.0.2
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
liumiaocn:flask liumiao$
代码示例:HTTP谓词(GET)
就像angular的插值表达式在模版中的作用一样,在flask中也可以一样使用,如果不熟悉angular的插值表达式的话也不要紧,看完下面的例子,基本上就会有一个大致的印象。
代码示例
liumiaocn:flask liumiao$ cat flask_4.py
#!/usr/bin/python
from flask import Flask
from flask import render_template
app = Flask(__name__)
greeting_messages=["Hello World", "Hello Python"]
@app.route("/api/messages",methods=['GET'])
def get_messages():
return render_template("resttest.html",messages=greeting_messages)
if __name__ == "__main__":
app.debug=True
app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$
模版文件
liumiaocn:flask liumiao$ cat templates/resttest.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Restful</title>
</head>
<body>
{% for message in messages %}
<h1>{{ message }}</h1>
{% endfor %}
</body>
</html>
liumiaocn:flask liumiao$
代码解析:app.route中指定了HTTP谓词GET,缺省GET可以省略,如果一个方法对应多个谓词动作,通过request.method来分离时,可以写成methods=[‘GET','POST']的形式
执行&确认
liumiaocn:flask liumiao$ ./flask_4.py
* Serving Flask app "flask_4" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 131-533-062
页面确认
代码示例:HTTP谓词(DELETE|PUT|POST)
liumiaocn:flask liumiao$ cat flask_4.py
#!/usr/bin/python
from flask import Flask
from flask import render_template
from flask import request
import json
app = Flask(__name__)
greeting_messages=["Hello World", "Hello Python"]
#HTTP: GET: Retrieve operation
@app.route("/api/messages",methods=['GET'])
def get_messages():
return render_template("resttest.html",messages=greeting_messages)
#HTTP: DELETE: Delete operation
@app.route("/api/messages/<messageid>",methods=['DELETE'])
def delete_message(messageid):
global greeting_messages
del greeting_messages[int(messageid)]
return render_template("resttest.html",messages=greeting_messages)
#HTTP: PUT: Update operation
#HTTP: POST: Create operation
@app.route("/api/messages/<messageid>",methods=['PUT','POST'])
def update_message(messageid):
global greeting_message
msg_info=json.loads(request.get_data(True,True,False))
#msg_info=request.args.get('message_info')
#msg_info=request.form.get('message_info','default value')
#msg_info=request.values.get('message_info','hello...')
greeting_messages.append("Hello " + msg_info["message_info"])
return render_template("resttest.html",messages=greeting_messages)
if __name__ == "__main__":
app.debug=True
app.run(host='0.0.0.0',port=7000)
liumiaocn:flask liumiao$
执行&结果确认
执行日志
liumiaocn:flask liumiao$ ./flask_4.py
* Serving Flask app "flask_4" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 131-533-062
结果确认:Delete
liumiaocn:flask liumiao$ curl -X DELETE http://localhost:7000/api/messages/1
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Restful</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>liumiaocn:flask liumiao$
可以看到执行一次DELETE之后,两条消息现在只剩下一条消息了,接下来使用POST添加再添加一条
liumiaocn:flask liumiao$ curl -X POST -d '{"message_info":"LiuMiaoPost"}' http://localhost:7000/api/messages/3
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Restful</title>
</head>
<body>
<h1>Hello World</h1>
<h1>Hello LiuMiaoPost</h1>
</body>
</html>liumiaocn:flask liumiao$
再执行一次PUT操作
liumiaocn:flask liumiao$ curl -X PUT -d '{"message_info":"LiuMiaoPut"}' http://localhost:7000/api/messages/4
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello Restful</title>
</head>
<body>
<h1>Hello World</h1>
<h1>Hello LiuMiaoPost</h1>
<h1>Hello LiuMiaoPut</h1>
</body>
</html>liumiaocn:flask liumiao$
小结
这篇文章中,使用最简单的方式在flask中模拟了一下如何进行Restful的CRUD操作,当然,实际的做法有很多种,在接下来的文章中还会介绍另外一种非常常见的轮子flask-restful.
来源:https://blog.csdn.net/liumiaocn/article/details/80728020
标签:python,flask,restful,crud
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
六行python代码的爱心曲线详解
2022-04-09 23:15:17
![](https://img.aspxhome.com/file/2023/3/114663_0s.png)
pytorch中的weight-initilzation用法
2022-03-22 23:28:24
Python3 ffmpeg视频转换工具使用方法解析
2021-07-28 15:00:30
![](https://img.aspxhome.com/file/2023/8/99038_0s.png)
asp中提示至少一个参数没有被指定值 解决方法
2013-06-01 20:14:00
Python OpenCV简单的绘图函数使用教程
2023-08-02 23:22:22
![](https://img.aspxhome.com/file/2023/7/61197_0s.jpg)
Python实现PDF转MP3的示例代码
2021-08-07 21:21:06
![](https://img.aspxhome.com/file/2023/2/95152_0s.png)
flask开启多线程的具体方法
2023-03-10 06:30:50
Python使用Py2neo创建Neo4j的节点和关系
2023-07-20 02:51:45
![](https://img.aspxhome.com/file/2023/5/101025_0s.png)
python使用matplotlib绘制折线图教程
2022-06-28 11:26:37
![](https://img.aspxhome.com/file/2023/3/99563_0s.png)
对pandas的算术运算和数据对齐实例详解
2023-05-29 07:24:51
Python实现字典去除重复的方法示例
2021-02-24 21:52:14
对python条件表达式的四种实现方法小结
2023-11-05 17:01:58
python如何提升爬虫效率
2021-12-17 22:18:24
Python 设计模式中命令模式
2022-10-14 20:28:36
![](https://img.aspxhome.com/file/2023/9/101659_0s.png)
Python中POST调用Restful接口示例
2021-03-14 19:42:19
Python线性回归图文实例详解
2023-04-06 07:31:42
![](https://img.aspxhome.com/file/2023/3/127613_0s.png)
解决seaborn在pycharm中绘图不出图的问题
2023-11-29 02:22:52
解决django中ModelForm多表单组合的问题
2021-06-03 04:13:14
![](https://img.aspxhome.com/file/2023/9/111879_0s.jpg)
Python采用Django制作简易的知乎日报API
2023-10-07 13:02:34
![](https://img.aspxhome.com/file/2023/6/63546_0s.jpg)
深入解析pandas数据聚合和重组
2023-11-17 17:45:51
![](https://img.aspxhome.com/file/2023/4/75154_0s.png)