零基礎(chǔ)學(xué)起
學(xué)習(xí)flask我選擇了 pycharm乏屯,學(xué)生的話可以免費下載專業(yè)版疑苔。廢話不多說了沦疾。
環(huán)境:python 3.6+
基礎(chǔ):0-
日期:2018.12.14
flask入門
簡單測試
pycharm安裝flask會自動導(dǎo)入了flask所需的模塊绒怨,所以我們只需要命令安裝所需要的包就可以了赦肋,建議用python3.6學(xué)習(xí)而不是2.7块攒,畢竟django都快要不支持2.7了,早換早超生佃乘。
自動導(dǎo)入的也是python 3.6囱井。
運行這邊會出小錯,因為此時我們還沒有安裝flask包趣避,
這樣就可以正常運行了庞呕,運行成功便會返回
?????* Debug mode: off
?????* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [14/Dec/2018 20:32:20] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [14/Dec/2018 20:32:20] "GET /favicon.ico HTTP/1.1" 404 -
此時可以在web上運行hello world了,訪問http://127.0.0.1:5000∽×罚可以看到打印出Hello World
route裝飾器路由
????@app.route('/')
使用 route() 裝飾器告訴 Flask 什么樣的URL 能觸發(fā)我們的函數(shù)地啰。 route() 裝飾器把一個函數(shù)綁定到對應(yīng)的 URL 上,這句話相當(dāng)于路由讲逛,一個路由跟隨一個函數(shù)亏吝,如
@app.route('/')
def test()"
???? return 123
訪問127.0.0.1:5000/ 則會輸出123,我們修改一下規(guī)則
@app.route('/test')
def test()"
?? return 123
這個時候訪問127.0.0.1:5000/test 會輸出123盏混。
此外還可以設(shè)置動態(tài)url蔚鸥,
@app.route("/hello/<username>")
def hello_user(username):
?? return "user:%s"%username
根據(jù)url里的輸入,動態(tài)辨別身份许赃,此時便可以看到如下頁面:
或者可以使用int型止喷,轉(zhuǎn)換器有下面幾種:
????int????接受整數(shù)
????float????同 int ,但是接受浮點數(shù)
????path????和默認(rèn)的相似混聊,但也接受斜線
????@app.route('/post/<int:post_id>')
????def show_post(post_id):
????????# show the post with the given id, the id is an integer
????????return 'Post %d' % post_id
main入口
當(dāng).py文件被直接運行時弹谁,if __name__ == '__main__'之下的代碼塊將被運行;當(dāng).py文件以模塊形式被導(dǎo)入時技羔,if __name__ == '__main__'之下的代碼塊不被運行。如果你經(jīng)常以cmd方式運行自己寫的python小腳本卧抗,那么不需要這個東西藤滥,但是如果需要做一個稍微大一點的python開發(fā),寫 if __name =='__main__' 是一個良好的習(xí)慣社裆,大一點的python腳本要分開幾個文件來寫拙绊,一個文件要使用另一個文件,也就是模塊泳秀,此時這個if就會起到作用不會運行而是類似于文件包含來使用标沪。
if __name__ == '__main__':
app.debug = True
app.run()
測試的時候,我們可以使用debug嗜傅,方便調(diào)試金句,增加一句
????app.debug = True
????或者(效果是一樣的)
????app.run(debug=True)
這樣我們修改代碼的時候直接保存,網(wǎng)頁刷新就可以了吕嘀,如果不加debug违寞,那么每次修改代碼都要運行一次程序,并且把前一個程序關(guān)閉偶房。否則會被前一個程序覆蓋趁曼。
????app.run(host='0.0.0.0')
這會讓操作系統(tǒng)監(jiān)聽所有公網(wǎng) IP,此時便可以在公網(wǎng)上看到自己的web。
http請求方法
頁面需要get請求或者post請求也可以由路由來解決棕洋,通過 route() 裝飾器傳遞 methods 參數(shù)挡闰。如我們要在登錄頁面使用get或者post登錄輸入。
????@app.route('/login', methods=['GET', 'POST'])
????def login():
if request.method == 'POST':
?? do_the_login()
else:
?? show_the_login_form()
模版渲染
何為模版渲染??點擊連接--->>>??https://shuaizhupeiqi.github.io/2018/11/11/SSTI%E6%A8%A1%E6%9D%BF%E6%B3%A8%E5%85%A5/
你可以使用 render_template() 方法來渲染模板。你需要做的一切就是將模板名和你想作為關(guān)鍵字的參數(shù)傳入模板的變量摄悯。這里有一個展示如何渲染模板的簡例:
簡單的模版渲染示例
????from flask import render_template
????@app.route('/hello/')
????@app.route('/hello/<name>')
????def hello(name=None):
return render_template('hello.html', name=name)
flask簡單實例
我們從模板渲染開始實例赞季,因為我們畢竟不是做開發(fā)的,flask以模板注入聞名射众,所以我們先從flask模版渲染入手深入剖析碟摆。
首先要搞清楚,模板渲染體系叨橱,render_template函數(shù)渲染的是templates中的模板典蜕,所謂模板是我們自己寫的html,里面的參數(shù)需要我們根據(jù)每個用戶需求傳入動態(tài)變量罗洗。
????├── app.py??
????├── static??
????│? ? ? ? └── style.css??
????└── templates??
????? ? ? ? ? └── index.html??
我們寫一個index.html文件寫templates文件夾中愉舔。
????<html>
??????<head>
????????<title>{{title}} - 小豬佩奇</title>
??????</head>
?????<body>
??????????<h1>Hello, {{user.name}}!</h1>
??????</body>
????</html>
里面有兩個參數(shù)需要我們渲染,user.name伙菜,以及title
我們在app.py文件里進(jìn)行渲染轩缤。
????@app.route('/')
????@app.route('/index')#我們訪問/或者/index都會跳轉(zhuǎn)
????def index():
???????user = {'name': '小豬佩奇'}#傳入一個字典數(shù)組
???????return render_template("index.html",title='Home',user=user)
這次渲染我們沒有使用用戶可控,所以是安全的贩绕,如果我們交給用戶可控并且不過濾參數(shù)就有可能造成SSTI模板注入漏洞火的。
jinja2模板同樣支持控制語句,在{% %}中輸入我們的代碼淑倾。如
????{% if title %}
????<title>{{title}} - xzpq</title>
????{% else %}
????<title>Welcome to xzpq</title>
????{% endif %}
如果傳入title參數(shù)那么執(zhí)行{{title}} - xzpq 否則執(zhí)行Welcome to xzpq 最后執(zhí)行下面未寫出馏鹤。
本文暫且到這里,主要是為了配合ssti模板注入而了解flask注入娇哆∨壤郏看繼續(xù)參考博文SSTI模板注入。
測試的代碼貼上
????from flask import Flask
????from flask import render_template
????from flask import request
????from flask import render_template_string
????app = Flask(__name__)
????@app.route('/login', methods=['GET', 'POST'])
????def login():
????????if request.method == 'POST':
????????????do_the_login()
????????else:
????????????show_the_login_form()
????@app.route('/',methods=['GET', 'POST'])
????@app.route('/index',methods=['GET', 'POST'])#我們訪問/或者/index都會跳轉(zhuǎn)
????def index():
???????return render_template("index.html",title='Home',user=request.args.get("key"))
????@app.route('/test',methods=['GET', 'POST'])
????def test():
????????template = '''
????????????<div class="center-content error">
????????????????<h1>Oops! That page doesn't exist.</h1>
????????????????<h3>%s</h3>
????????????</div>?
????????''' %(request.url)
????????return render_template_string(template)
????if __name__ == '__main__':
????????app.debug = True
????????app.run()
---
參考:http://www.pythondoc.com/flask-mega-tutorial/