4.3 获取请求参数
from flask import request
就是 Flask 中表示当前请求的 request 对象,request对象中保存了一次HTTP请求的一切信息。前三个比较常用:
在python2中在非英文字符前加字母’u’可以解决编码错误问题
U’中国’ % sa
# 接口 api
# 127.0.0.1:5000/index?city=shenzhen&country=china 查询字符串 QueryString
@app.route("/index", methods=["GET", "POST"])
def index():
# request中包含了前端发送过来的所有请求数据
# form和data是用来提取请求体数据
# 通过requset.form可以直接提取请求体中的表单格式的数据, 是一个类字典的对象
# 通过get方法只能拿到多个同名参数的第一个
name = request.form.get("name")
age = request.form.get("age")
name_li = request.form.getlist("name")
# 如果是请求体的数据不是表单格式的(如json格式),可以通过request.data获取
print("request.data: %s" % request.data)
# args是用来提取url中的参数(查询字符串)
city = request.args.get("city")
return "hello name=%s, age=%s, city=%s, name_li=%s" % (name, age, city, name_li)
4.3.1 上传文件
已上传的文件存储在内存或是文件系统中一个临时的位置。你可以通过请求对象的 files 属性访问它们。每个上传的文件都会存储在这个字典里。它表现近乎为一个标准的 Python file 对象,但它还有一个 save() 方法,这个方法允许你把文件保存到服务器的文件系统上。这里是一个用它保存文件的例子:
from flask import request
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['the_file']
f.save('/var/www/uploads/uploaded_file.txt')
# coding:utf-8
from flask import Flask, request
app = Flask(__name__)
@app.route("/upload", methods=["POST"])
def upload():
"""接受前端传送过来的文件"""
file_obj = request.files.get("pic")
if file_obj is None:
# 表示没有发送文件
return "未上传文件"
# 将文件保存到本地
# # 1. 创建一个文件
# f = open("./demo.png", "wb")
# # 2. 向文件写内容
# data = file_obj.read()
# f.write(data)
# # 3. 关闭文件
# f.close()
# 直接使用上传的文件对象保存,相当于上面的文件操作open(),close()进行封装
file_obj.save("./demo1.png")
return "上传成功"
if __name__ == '__main__':
app.run(debug=True)
如果你想知道上传前文件在客户端的文件名是什么,你可以访问 filename 属性。但请记住, 永远不要信任这个值,这个值是可以伪造的。如果你要把文件按客户端提供的文件名存储在服务器上,那么请把它传递给 Werkzeug 提供的 secure_filename() 函数:
from flask import request
from werkzeug import secure_filename
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['the_file']
f.save('/var/www/uploads/' + secure_filename(f.filename))
...
上下文件管理器(with)
class Foo(object):
def __enter__(self):
"""进入with语句的时候被with调用"""
print("enter called")
def __exit__(self, exc_type, exc_val, exc_tb):
"""离开with语句的时候被with调用"""
print("exit called")
print("exc_type: %s" % exc_type)
print("exc_val: %s" % exc_val)
print("exc_tb: %s" % exc_tb)
with Foo() as foo:
print("hello python")
a = 1 / 0
print("hello end")
# 进入with语句的时候,with帮助我们调用对象的__enter__方法,
# 离开with语句的时候,with帮助我们调用对象的__exit__方法4.4 abort函数与自定义异常处理
4.4.1 abort函数
From flask import Flask,request,abort,Response
@app.route("/login", methods=["GET"])
def login():
# name = request.form.get()
# pwd = request.form.get()
name = ""
pwd = ""
if name != "zhangsan" or pwd != "admin":
# 使用abort函数可以立即终止视图函数的执行
# 并可以返回给前端特定的信息
# 1 传递状态码信息, 必须是标准的http状态码
abort(404)
# # 2. 传递响应体信息
# resp = Response("login failed")
# abort(resp)
return "login success"
4.4.2 自定义异常处理
@app.errorhandler(404)
def error(e):
return '您请求的页面不存在了,请确认后再次访问!%s'%e
4.5 返回的响应数据
4.5.1 元组
可以返回一个元组,这样的元组必须是 (response, status, headers) 的形式,且至少包含一个元素。 status 值会覆盖状态代码, headers 可以是一个列表或字典,作为额外的消息标头值。
@app.route("/index")
def index():
# 1 使用元祖,返回自定义的响应信息
# 响应体 状态码 响应头
# return "index page", 400, [("Itcast", "pyton"), ("City", "shenzhen")]
# return "index page", 400, {"Itcast1": "python1", "City1": "sz1"}
# return "index page", 666, {"Itcast1": "python1", "City1": "sz1"}
# return "index page", "666 itcast status", {"Itcast1": "python1", "City1": "sz1"}
# return "index page", "666 itcast status"
# 2 使用make_response 来构造响应信息
resp = make_response("index page 2")
resp.status = "999 itcast" # 设置状态码
resp.headers["city"] = "sz" # 设置响应头
return resp
4.5.2 make_response
resp = make_response()
resp.headers[“sample”] = “value”
resp.status = “404 not found”
4.6 使用jsonify返回json数据
from flask import Flask, jsonify
import json
app = Flask(__name__)
@app.route("/index")
def index():
# json就是字符串
data = {
"name": "python",
"age": 24
}
# # json.dumps(字典) 将python的字典转换为json字符串
# # json.loads(字符串) 将字符串转换为python中的字典
#
# json_str = json.dumps(data)
#
# return json_str, 200, {"Content-Type": "application/json"}
# jsonify帮助转为json数据,并设置响应头 Content-Type 为application/json
# return jsonify(data)
return jsonify(city="sz", country="china")
4.5 重定向
from flask import redirect
4.6 设置和读取cookie
From flask import make_response
@app.route("/set_cookie")
def set_cookie():
resp = make_response("success")
# 设置cookie, 默认有效期是临时cookie,浏览器关闭就失效
resp.set_cookie("Itcast", "Python")
resp.set_cookie("Itcast1", "Python1")
# max_age设置有效期,单位:秒
resp.set_cookie("Itcast2", "Python1", max_age=3600)
resp.headers["Set-Cookie"] = "Itcast3=Python3; Expires=Sat, 18-Nov-2017 04:36:04 GMT; Max-Age=3600; Path=/"
return resp
@app.route("/get_cookie")
def get_cookie():
c = request.cookies.get("Itcast")
return c
@app.route("/delete_cookie")
def delete_cookie():
resp = make_response("del success")
# 删除cookie
resp.delete_cookie("Itcast1")
return resp4.7 session
设置和读取session
from flask import session,
# flask的session需要用到的秘钥字符串
app.config["SECRET_KEY"] = "dhsodfhisfhosdhf29fy989"
# flask默认把session保存到了cookie中
@app.route("/login")
def login():
# 设置session数据
session["name"] = "python"
session["mobile"] = "18611111111"
return "login success"
@app.route("/index")
def index():
# 获取session数据
name = session.get("name")
return "hello %s" % name
4.8 请求上下文与应用上下文
线程局部变量
请求上下文(request context)
request和session都属于请求上下文对象。
应用上下文(application context)
current_app和g都属于应用上下文对象。
current_app:表示当前运行程序文件的程序实例。
g:处理请求时,用于临时存储的对象,每次请求都会重设这个变量。
4.9 请求钩子
请求钩子是通过装饰器的形式实现,Flask支持如下四种请求钩子:
before_first_request:在处理第一个请求前运行。
@app.before_first_request
before_request:在每次请求前运行。
after_request(response):如果没有未处理的异常抛出,在每次请求后运行。
teardown_request(response):在每次请求后运行,即使有未处理的异常抛出。
@app.route("/index")
def index():
print("index 被执行")
a = 1 / 0
return "index page"
@app.route("/hello")
def hello():
print("hello 被执行")
return "hello page"
@app.before_first_request
def handle_before_first_request():
"""在第一次请求处理之前先被执行"""
print("handle_before_first_request 被执行")
@app.before_request
def handle_before_request():
"""在每次请求之前都被执行"""
print("handle_before_request 被执行")
@app.after_request
def handle_after_request(response):
"""在每次请求(视图函数处理)之后都被执行, 前提是视图函数没有出现异常"""
print("handle_after_request 被执行")
return response
@app.teardown_request
def handle_teardown_request(response):
"""在每次请求 (视图函数处理)之后都被执行, 无论视图函数是否出现异常,都被执行, 工作在非调试模式时 debug=False"""
“””请求的路径”””
path = request.path
if path == url_for("index"):
print("在请求钩子中判断请求的视图逻辑: index")
elif path == url_for("hello"):
print("在请求钩子中判断请求的视图逻辑: hello")
print("handle_teardown_request 被执行")
return response
5. Flask-Script扩展命令行
pip install Flask-Script
from flask import Flask
from flask_script import Manager
app = Flask(__name__)
manager = Manager(app)
@app.route('/')
def index():
return '床前明月光'
if __name__ == "__main__":
manager.run() ...
python 10_flask_script.py --help
python 10_flask_scrip.py runserver
>>> app.url_map