Python Web Infrastructure - Flask(1)

##########################################################################################
#########Python script: 創建web app. 包含3個頁面的處理:主頁面,申請頁面,審批頁面########
##########################################################################################

from flask import Flask
from flask_wtf import FlaskForm
from wtforms import TextField, SubmitField,IntegerField, BooleanField,SelectField,DecimalField,TextAreaField,PasswordField

f_instance=Flask(__name__)

@f_instance.route('/')
def index():
    
    return render_template("home.html" )

@f_instance.route('/apply',methods=['GET','POST'])
def apply_input():
    form=applyform()
    return render_template("apply_input.html",form=form)

@f_instance.route('/approve',methods=['GET','POST'])
def approve_view():
    form=approveform()
    if request.method=='GET':
        apply=request.args
    else
        apply=request.form
    return render_template("approve_view.html", form=form,apply=apply)

f_instance.config['SECRET_KEY']='flasksecret'
if __name__=="__main__":
    f_instance.run(debug=True)

 

####################################################################
##########htlm page: 包含3個頁面:主頁面,申請頁面,審批頁面########
####################################################################

########## 主頁面 ########

<!DOCTYPE html>
<html lang="en"> 

<body>  
    <h1>Welcome!</h1>
    <a href={{url_for("apply_input")}}>Submit your appllication please.</a>  
  </body>
</html>

 

########## 申請頁面 ########
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <form action={{url_for('approve_view')}} method="GET">
    {{form.hidden_tag()}}
    {{form.apply.label}}{{form.apply}}
    {{form.submit}}    
    </form>
  </body>
</html>

########## 審批頁面 ########
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    {% for key, value in apply.items() %}
      {{key}}:{{value}}<br>
    {% endfor %}
    <form action={{url_for('index')}}>    
    {{form.submit}}    
    </form>
  </body>
</html>

上面顯示出Flask通過在request中添加參數csrf_token來預防csrf攻擊。GET方法中該參數出現在url中,POST方法中該參數出現在form中。

CSRF介紹:https://blog.csdn.net/ru_li/article/details/53301708

CSRF攻擊只是盜用用戶的身份訪問服務器執行非法更新操作,比如冒充用戶對銀行賬戶轉賬,該攻擊無法知道用戶的身份信息,對於攻擊返回的信息也無法查看,所以對於只讀操作無效。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章