Flask: 请求与响应

Flask 请求

Request 请求对象

Request 请求对象封装了从客户端发来的请求报文,可以从其中获取请求报文的所有数据。

Request 亲请求对象的常用属性方法如下

属性或方法 说明
forms 一个字典,储存请求所提交的表单字段
args 一个字典,储存通过 URL 查询字符串传递的所有参数
values 一个字典,form 和 args 的合集
cookies 一个字典,存储请求的所有 cookie
headers 一个字典,存储所有请求的 HTTP 首部
files 一个字典,存储请求的上传的所有文件
get_data() 返回请求主体的缓冲的数据
get_json 返回一个 Python 字典,包含解析请求主体后得到的 JSON
blueprint 返回请求的 Flak 蓝本的名称
endpoint 处理请求的 Flask 端点的名称
method HTTP 请求方法,可以是 GET 或 POST
scheme URL 方案 (http 或 https)
is_secure 通过安全的连接 (HTTPS) 发送请求是,返回 True
host 请求定义的主机名,如果客户端定义了端口号,还包括端口号
path URL 路径部分
query_string URL 的查询字符串部分,返回原始二进制值
full_path URL 的路径和查询字符串部分
url 客户端请求的完整 URL
base_url 同 url, 但没有查询字符串部分
remote_addr 客户端的 IP 地址
environ 请求原始 WSGI 环境字典

获取 GET

使用 request.args.get() 方法获取 GET 请求参数

1
2
3
4
5
6
7
8
9
10
11
from flask import *

app=Flask(__name__)
@app.route("/")
def index():
name=request.args.get('name')
age=request.args.get('age')
message=f"姓名{name}\n年龄{age}".format(name=name, age=age)
return message
if __name__ == '__main__':
app.run()

获取 POST 请求参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests
from flask import *

app=Flask(__name__)
@app.route("/login",methods=["GET", "POST"])
def login():
if request.method=='POST':
username=request.form['username']
password=request.form['password']
message=f"用户名是{username},密码是{password}"
return message
return render_template('login.html')
if __name__=='__main__':
app.run()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!doctype html>
<html>
<head>
<title>登录</title>
</head>
<body>
<center>表单</center><hr>
<form action="" method="post">
<input type="text" id="username" name="username" value=""><br>
<input type="password" id="password" name="password" value="">
<button type="submit">提交</button>
</form>
</body>
</html>

文件上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os.path
import uuid
import os

from flask import *

def random_file(filename):
ext=os.path.splitext(filename)[1]
new_filename=uuid.uuid4().hex+ext
return new_filename
app=Flask(__name__)
@app.route('/upload', methods=['GET','POST'])
def upload():
if request.method=='POST':
avatar = request.files['avatar']
if avatar:
filename=random_file(avatar.filename)
avatar.save(os.path.join("upload", filename))
return redirect(url_for('uploaded_file',filename=filename))
return render_template('\\upload.html')
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory("upload", filename)
@app.route("/login",methods=["GET", "POST"])
def login():
if request.method=='POST':
username=request.form['username']
password=request.form['password']
message=f"用户名是{username},密码是{password}"
return message
return render_template('login.html')
if __name__=='__main__':
app.run()
1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="avatar" name="avatar" value=""><br>
<button type="submit">提交</button>
</form>
</body>
</html>