Python編程在flask中模擬進(jìn)行Restful的CRUD操作
這篇文章中我們將通過對(duì)HelloWorld的message進(jìn)行操作,介紹一下如何使用flask進(jìn)行Restful的CRUD。
概要信息

事前準(zhǔn)備: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的插值表達(dá)式在模版中的作用一樣,在flask中也可以一樣使用,如果不熟悉angular的插值表達(dá)式的話也不要緊,看完下面的例子,基本上就會(huì)有一個(gè)大致的印象。
代碼示例
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可以省略,如果一個(gè)方法對(duì)應(yīng)多個(gè)謂詞動(dòng)作,通過request.method來分離時(shí),可以寫成methods=[‘GET','POST']的形式
執(zhí)行&確認(rèn)
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
頁面確認(rèn)

代碼示例: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$
執(zhí)行&結(jié)果確認(rèn)
執(zhí)行日志
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
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)在只剩下一條消息了,接下來使用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$
再執(zhí)行一次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$
小結(jié)
這篇文章中,使用最簡單的方式在flask中模擬了一下如何進(jìn)行Restful的CRUD操作,當(dāng)然,實(shí)際的做法有很多種,在接下來的文章中還會(huì)介紹另外一種非常常見的輪子flask-restful.
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- python使用 request 發(fā)送表單數(shù)據(jù)操作示例
- Python mutiprocessing多線程池pool操作示例
- python3+requests接口自動(dòng)化session操作方法
- Python讀取properties配置文件操作示例
- Python中用psycopg2模塊操作PostgreSQL方法
- 使用Python對(duì)Access讀寫操作
- Python操作Access數(shù)據(jù)庫基本步驟分析
- python進(jìn)程類subprocess的一些操作方法例子
- Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫教程
- 基于python操作ES實(shí)例詳解
相關(guān)文章
Python摳圖教程之使用OpenCV實(shí)現(xiàn)背景去除
這篇文章主要給大家介紹了關(guān)于Python摳圖教程之使用OpenCV實(shí)現(xiàn)背景去除的相關(guān)資料,背景去除是在很多視覺應(yīng)用里的主要預(yù)處理步驟,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
如何在keras中添加自己的優(yōu)化器(如adam等)
這篇文章主要介紹了在keras中實(shí)現(xiàn)添加自己的優(yōu)化器(如adam等)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python爬蟲實(shí)戰(zhàn)之爬取京東商城實(shí)例教程
這篇文章主要介紹了python爬取京東商城的相關(guān)資料,文中通過爬取一個(gè)實(shí)例頁面進(jìn)行了講解,通過示例代碼和圖文介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-04-04
Python3之外部文件調(diào)用Django程序操作model等文件實(shí)現(xiàn)方式
這篇文章主要介紹了Python3之外部文件調(diào)用Django程序操作model等文件實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
python中Array和DataFrame相互轉(zhuǎn)換的實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于python中Array和DataFrame相互轉(zhuǎn)換的實(shí)例講解內(nèi)容,對(duì)此有需要的朋友們可以學(xué)參考下。2021-02-02
pandas.DataFrame中提取特定類型dtype的列
本文主要介紹了pandas.DataFrame中提取特定類型dtype的列,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python上傳時(shí)包含boundary時(shí)的解決方法
這篇文章主要介紹了python上傳時(shí)包含boundary時(shí)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
python調(diào)用java的Webservice示例
這篇文章主要介紹了python調(diào)用java的Webservice具體方法,包含java端和python實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-03-03

