最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python編程在flask中模擬進行Restful的CRUD操作

 更新時間:2018年12月28日 14:43:03   作者:liumiaocn  
今天小編就為大家分享一篇關于Python編程在flask中模擬進行Restful的CRUD操作,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

這篇文章中我們將通過對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']的形式

執(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

頁面確認

代碼示例: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í)行&結果確認

執(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

結果確認: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$

小結

這篇文章中,使用最簡單的方式在flask中模擬了一下如何進行Restful的CRUD操作,當然,實際的做法有很多種,在接下來的文章中還會介紹另外一種非常常見的輪子flask-restful.

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

相關文章

  • python實現(xiàn)碑帖圖片橫向拼接

    python實現(xiàn)碑帖圖片橫向拼接

    這篇文章主要為大家詳細介紹了python實現(xiàn)碑帖圖片橫向拼接,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • Python中l(wèi)ogging日志記錄到文件及自動分割的操作代碼

    Python中l(wèi)ogging日志記錄到文件及自動分割的操作代碼

    這篇文章主要介紹了Python中l(wèi)ogging日志記錄到文件及自動分割,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • Python使用修飾器進行異常日志記錄操作示例

    Python使用修飾器進行異常日志記錄操作示例

    這篇文章主要介紹了Python使用修飾器進行異常日志記錄操作,結合實例形式分析了Python基于修飾器的log日志文件操作的相關實現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • python requests抓取one推送文字和圖片代碼實例

    python requests抓取one推送文字和圖片代碼實例

    這篇文章主要介紹了python requests抓取one推送文字和圖片代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • python interpolate插值實例

    python interpolate插值實例

    這篇文章主要介紹了python interpolate插值實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • python神經網(wǎng)絡slim常用函數(shù)訓練保存模型

    python神經網(wǎng)絡slim常用函數(shù)訓練保存模型

    這篇文章主要為大家介紹了python神經網(wǎng)絡使用slim函數(shù)進行模型的訓練及保存模型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • 說一說Python logging

    說一說Python logging

    這篇文章主要和大家聊一聊Python logging,Python logging是什么,Python logging的作用是什么,感興趣的小伙伴們可以參考一下
    2016-04-04
  • python發(fā)送多人郵件沒有展示收件人問題的解決方法

    python發(fā)送多人郵件沒有展示收件人問題的解決方法

    這篇文章主要為大家詳細介紹了python發(fā)送多人郵件沒有展示收件人問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Python正則表達式re.findall()幾種方法示例

    Python正則表達式re.findall()幾種方法示例

    這篇文章主要介紹了Python正則表達式re.findall()幾種方法示例,文中介紹了如何使用正則表達式在字符串中查找匹配的字符,并將它們返回為一個列表,示例展示了如何匹配數(shù)字并處理沒有匹配到的情況,需要的朋友可以參考下
    2024-12-12
  • Python中的數(shù)據(jù)標準化與反標準化全面指南

    Python中的數(shù)據(jù)標準化與反標準化全面指南

    在數(shù)據(jù)處理和機器學習中,數(shù)據(jù)標準化是一項至關重要的預處理步驟,標準化能夠將不同尺度和范圍的數(shù)據(jù)轉換為相同的標準,有助于提高模型的性能和穩(wěn)定性,Python提供了多種庫和函數(shù)來執(zhí)行數(shù)據(jù)標準化和反標準化,如Scikit-learn和TensorFlow
    2024-01-01

最新評論

玉林市| 眉山市| 抚远县| 阿坝县| 合江县| 邯郸市| 阿鲁科尔沁旗| 阜阳市| 桃园县| 百色市| 汕头市| 喀喇| 朝阳市| 海晏县| 新野县| 九江市| 老河口市| 郴州市| 惠州市| 成都市| 涿鹿县| 开阳县| 资源县| 黄石市| 讷河市| 城口县| 闵行区| 山东| 瑞安市| 高邑县| 梅州市| 子长县| 塔河县| 兖州市| 新绛县| 资阳市| 鸡西市| 永宁县| 阳高县| 七台河市| 宣恩县|