激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

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

服務(wù)器之家 - 腳本之家 - Python - Python編程在flask中模擬進(jìn)行Restful的CRUD操作

Python編程在flask中模擬進(jìn)行Restful的CRUD操作

2021-05-09 00:33liumiaocn Python

今天小編就為大家分享一篇關(guān)于Python編程在flask中模擬進(jìn)行Restful的CRUD操作,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

這篇文章中我們將通過(guò)對(duì)helloworld的message進(jìn)行操作,介紹一下如何使用flask進(jìn)行restful的crud。

概要信息

Python編程在flask中模擬進(jìn)行Restful的CRUD操作

事前準(zhǔn)備: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的插值表達(dá)式在模版中的作用一樣,在flask中也可以一樣使用,如果不熟悉angular的插值表達(dá)式的話也不要緊,看完下面的例子,基本上就會(huì)有一個(gè)大致的印象。

代碼示例

?
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可以省略,如果一個(gè)方法對(duì)應(yīng)多個(gè)謂詞動(dòng)作,通過(guò)request.method來(lái)分離時(shí),可以寫成methods=[‘get','post']的形式

執(zhí)行&確認(rèn)

?
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

頁(yè)面確認(rèn)

Python編程在flask中模擬進(jìn)行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$

執(zhí)行&結(jié)果確認(rèn)

執(zhí)行日志

?
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

結(jié)果確認(rèn):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$

可以看到執(zhí)行一次delete之后,兩條消息現(xiàn)在只剩下一條消息了,接下來(lái)使用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$

再執(zhí)行一次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$

小結(jié)

這篇文章中,使用最簡(jiǎn)單的方式在flask中模擬了一下如何進(jìn)行restful的crud操作,當(dāng)然,實(shí)際的做法有很多種,在接下來(lái)的文章中還會(huì)介紹另外一種非常常見(jiàn)的輪子flask-restful.

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

原文鏈接:https://blog.csdn.net/liumiaocn/article/details/80728020

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品美乳 | 国产亚洲精品成人 | 人人舔人人射 | 欧美日韩免费一区二区三区 | 久久99精品久久久久久秒播放器 | 国内精品久久久久久久影视红豆 | 久久久成人免费视频 | av在线免费看片 | 亚洲网站免费观看 | 操操插插 | 久草在线精品观看 | 色婷婷久久久亚洲一区二区三区 | 欧美日韩亚洲不卡 | 羞羞色院91精品网站 | 91情侣在线偷精品国产 | 欧美一级精品片在线看 | 99爱视频在线 | 深夜免费福利视频 | 欧美a久久| 日本综合久久 | 欧美亚洲一区二区三区四区 | 在线观看免费污视频 | 中文区永久区 | 在线看一区二区三区 | 国产亚洲精品久久久久5区 男人天堂免费 | 国产精品一区二区三区在线播放 | 国产精品成人亚洲一区二区 | 羞羞的| 亚洲成人在线免费观看 | 精品国产一二区 | 中国漂亮护士一级a毛片 | 破处av在线 | 久久蜜桃香蕉精品一区二区三区 | 日韩在线欧美在线 | 精品在线免费播放 | 中文字幕亚洲视频 | 中文有码一区二区 | 爱草成年| 九九热在线精品视频 | 一本色道久久综合亚洲精品小说 | 国产一级毛片高清视频完整版 |