flask 中的 request 对象用来获取请求信息
其中有以下
- args 所有 query 参数
- form 所有 get 参数
- cookies 所有 cookie 参数
from app import app
from flask import request, render_template_string, make_response
@app.route('/')
@app.route('/index')
def index():
return '''Hello World. <br>
click here to login: <a href="/login"> login </a> <br>
click here to query: <a href="/query?a=1&b=2"> query </a> <br>
click here to set cookie: <a href="/cookie"> set cookie </a>
'''
- post 数据
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
if username == 'kur0mi':
return 'login success.'
else:
return 'login failed.'
return '''
<form action="" method="post">
username: <input type="text" name="username">
<input type="submit" value="login">
</form>
'''
- get 数据
@app.route('/query')
def query():
return render_template_string('''
<table border=1>
{% for k, v in args.items() %}
<tr>
<th> {{ k }} </th>
<td> {{ v }} </td>
</tr>
{% endfor %}
</table>
''', args=request.args)
- cookie 数据
@app.route('/cookie', methods=['GET', 'POST'])
def cookie():
if request.method == 'POST':
user = request.form['user']
resp = make_response('''welcome to here to setcookie.
''')
resp.set_cookie('user', user)
return resp
return render_template_string('''
<form action="" method="post">
user: <input type="text" name="user">
<input type="submit" value="set cookie">
</form>
this are you cookies?
<table border=1>
{% for k, v in cookies.items() %}
<tr>
<th> {{ k }} </th>
<td> {{ v }} </td>
</tr>
{% endfor %}
</table>
''', cookies=request.cookies)