六、请求与响应

1、常用的请求信息

 1 @app.route("/home")
 2 def home():
 3     from flask import request
 4     # 常用请求信息
 5     print(request.method)
 6     print(request.args)
 7     print(request.form)
 8     print(request.files)
 9     print(request.cookies)
10     print(request.headers)
11     print(request.values)
12     print(request.data)
13     print(request.path)
14     print(request.full_path)
15     print(request.url)
16     print(request.base_url)
17     print(request.url_root)
18     print(request.host)
19     print(request.host_url)
20     print(request.script_root)
21     return "Hello, home!"

2、常用的构造响应的方式(响应编辑Cookie,请求获取Cookie)

 1 @app.route("/")
 2 def index():
 3     # 响应的几种形式
 4     # 返回字符串
 5     # return "Hello"
 6     # 返回一个int,根据报错信息得知
 7     # string, dict, tuple, Response instance, or WSGI callable, but it was a int
 8     # The tuple must have the form (body, status, headers), (body, status), or (body, headers).
 9     # return (1 , 2, 3, 4)
10     # 返回一个字典,必须是可序列化对象
11     # return {"func": [1, 2, 3]}
12     # 渲染模板
13     # from flask import render_template
14     # data = {}
15     # return render_template("home.html", **data)
16     # 重定向
17     # from flask import redirect
18     # return redirect("/home")
19 
20     # 构造一个响应
21     response = make_response("Hello World")
22     response.headers["token"] = "awdadjdkajdaadkjadjadja"
23     response.set_cookie("k", "v")
24     response.delete_cookie("session")
25     return response

 

posted @ 2020-07-24 15:49  找回失去的自我  阅读(215)  评论(0编辑  收藏  举报