国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产一级片播放 | 亚洲免费观看视频 | 国产在线视频一区 | 国产精品久久久久久久久久99 | 成人亚洲欧美 | 日本a v在线播放 | 黄视频在线免费看 | 色综合久久久久 | 亚洲欧洲精品成人久久奇米网 | 日韩精品一区二区三区在线 | 久久国内精品 | 久久亚洲精品国产精品紫薇 | 在线看的av | 国户精品久久久久久久久久久不卡 | 日韩欧美国产一区二区 | 久久久精品国产 | 国产一区在线视频 | 国产一区欧美 | 美女毛片 | 午夜国产| 欧美电影一区 | 午夜视频在线观看网站 | 亚洲va欧美va天堂v国产综合 | 欧美日韩精品网站 | 99中文字幕| 在线观看国产一区二区 | 国产精品久久一区二区三区 | 99久久夜色精品国产网站 | 欧美日韩中文国产一区发布 | 亚洲好色视频 | 欧美在线观看免费观看视频 | 中国黄色一级 | 亚洲第一se情网站 | 成人免费观看在线视频 | 亚洲精品在线视频 | 欧美精品国产精品 | 一区二区三区高清 | 国产精品久久久99 | 欧洲精品在线观看 | 在线观看中文字幕亚洲 | 蜜桃视频一区二区 |