flask简单入门
学习原因:ctf中有一些模板注入漏洞比如flask模板注入漏洞,学习这些漏洞前好歹要看得懂简单模板的框架再说。
基本格式
from flask import Flask
#写一个函数处理浏览器发送来的请求
@app.route("/")#页面根目录127.0.0.1:5000/
def index():
#
#这里处理业务逻辑
#
return "hello gay"#表示返回数据->相应
@app.route("/az")#页面127.0.0.1:5000/az
def az():
#
#这里处理业务逻辑
#
return "good job"
if __name__=='__main__':#程序入口
app.run()
登录案例
render_template(): 为返回html模板,模板文件需要放在py文件同级目录的templates文件夹中。
request.form.get():以字典通过键的方式获取form表单中以POST方式提交的数据
request.args.get():获取url后/?以GET方式提交的数据
app.route默认是GET
from flask import Flask,render_template,request
#创建应用程序
app=Flask(__name__)
#引用模板->html
@app.route("/")
def index():
s='my name is kiki'
return render_template("index_test.html",kiki=s)#返回html网页模板,html用{{jht}}接收
@app.route("/login",methods=['POST'])
def login():
#接收用户名,密码
#{username:名字,password:密码}
username=request.form.get("username")
password=request.form.get("password")
#request.args.get()#url传参
if username=='admin'and password=='password':
game_list=['拳皇1.72','狂扁小朋友无敌版','合金弹头','游戏王','flag{you_are_loging_!!}']
return render_template("index_test.html",game=game_list)
else:
return render_template("index_test.html",msg="登录失败")
if __name__=='__main__':#程序入口
app.run(debug=True,host='127.0.0.1',port='5001')#开始执行,调试不需要重启服务器
html
html文件中接收python代码需要使用 {{}}
循环语句格式为:
{% for item in game %}
<!--这里随便写-->
{% endfor %}
<!DOCTYPE html>
<html>
<head>
<title>{{kiki}}</title>
</head>
<body>
<form action="/login" method="POST">
用户名<input type="text" name="username"></br>
密码<input type="password" name="password"></br>
<input type="submit" value="登录">
</form>
{{msg}}
{% for item in game %}
<div>
{{item}}
</div></br>
{% endfor %}
</body>
</html>
完结撒花,下一步去学学flask注入。