脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python编程在flask中模拟进行Restful的CRUD操作

Python编程在flask中模拟进行Restful的CRUD操作

2021-05-09 00:33liumiaocn Python

今天小编就为大家分享一篇关于Python编程在flask中模拟进行Restful的CRUD操作,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

这篇文章中我们将通过对helloworld的message进行操作,介绍一下如何使用flask进行restful的crud。

概要信息

Python编程在flask中模拟进行Restful的CRUD操作

事前准备:flask

?
1
2
3
4
5
6
7
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的插值表达式的话也不要紧,看完下面的例子,基本上就会有一个大致的印象。

代码示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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$

模版文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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']的形式

执行&确认

?
1
2
3
4
5
6
7
8
9
10
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

页面确认

Python编程在flask中模拟进行Restful的CRUD操作

代码示例:http谓词(delete|put|post)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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$

执行&结果确认

执行日志

?
1
2
3
4
5
6
7
8
9
10
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

?
1
2
3
4
5
6
7
8
9
10
11
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添加再添加一条

?
1
2
3
4
5
6
7
8
9
10
11
12
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操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

延伸 · 阅读

精彩推荐