Flask后端笔记(二)request、bort、响应、session、钩子

获取请求参数

from flask import request

就是 Flask 中表示当前请求的 request 对象,request对象中保存了一次HTTP请求的一切信息。
在这里插入图片描述

python2 字符串类型
   str  "utf-8"  "gbk"
   unicode

a = "中国"   # str
a = u"中国"   # unicode

常见错误:ASCII  cannot decode \xxx\xx  解决方法都使用u"xxx"
u"中国"  % "sa"

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="post" enctype="multipart/form-data">
        <input type="text" name="city">
        <input  type="text" name="age">
        <input type="file" name="gender">
        <input type="submit">
    </form>
</body>
</html>

# 表单格式的数据
city=xxx&age=xxx&gender=xxx

request.py

# coding:utf-8

from flask import Flask, request


app = Flask(__name__)


# 接口  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)


# def register():
#     if request.method == 'GET':
#         return render(request, "register.html")
#     else:
#         


if __name__ == '__main__':
    app.run(debug=True)

上传文件

已上传的文件存储在内存或是文件系统中一个临时的位置。你可以通过请求对象的 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')
    ...

如果你想知道上传前文件在客户端的文件名是什么,你可以访问 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))

upload.py

# 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()

    # 直接使用上传的文件对象保存
    file_obj.save("./demo1.png")
    return "上传成功"


if __name__ == '__main__':
    app.run(debug=True)

_with的使用

# coding:utf-8


# f = open("./1.txt", "wb")
# # 2. 向文件写内容
# try:
#     f.write("hello flask")
# except Exception:
#     pass
# finally:
#     # 3. 关闭文件
#     f.close()


# 上下文管理器
# with open("./1.txt", "wb") as f:
#     f.write("hello flask")
#     f.write("hello flask")
#     f.write("hello flask")
#     f.write("hello flask")


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__方法

bort函数与自定义异常处理

abort函数

from flask import abort

自定义异常处理

@app.errorhandler(404)
def error(e):
    return '您请求的页面不存在了,请确认后再次访问!%s'%e
# coding:utf-8

from flask import Flask, request, abort, Response


app = Flask(__name__)


@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"


# 定义错误处理的方法
@app.errorhandler(404)
def handle_404_error(err):
    """自定义的处理错误方法"""
    # 这个函数的返回值会是前端用户看到的最终结果
    return u"出现了404错误, 错误信息:%s" % err


if __name__ == '__main__':
    app.run(debug=True)

返回的响应数据

元组

可以返回一个元组,这样的元组必须是 (response, status, headers) 的形式,且至少包含一个元素。 status 值会覆盖状态代码, headers 可以是一个列表或字典,作为额外的消息标头值。

make_response

resp = make_response()
resp.headers[“sample”] = “value”
resp.status =404 not found”
# coding:utf-8

from flask import Flask, request, abort, Response, make_response


app = Flask(__name__)


@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


if __name__ == '__main__':
    app.run(debug=True)

json模块的使用

在这里插入图片描述

使用jsonify返回json数据

# coding:utf-8

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")


if __name__ == '__main__':
    app.run(debug=True)


重定向

from flask import redirect

设置和读取cookie

make_response

set_cookie(key, value=’’, max_age=None)

delete_cookie(key)
# coding:utf-8

from flask import Flask, make_response, request


app = Flask(__name__)


@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)
    # 通过设置请求头 设置cookie
    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 resp


if __name__ == '__main__':
    app.run(debug=True)


session

from flask import session

需要设置secret_key

在这里插入图片描述
不使用cookie也可以通过Url设置session 弊端就是关闭浏览器session就会失效。
在这里插入图片描述
session跨服务器问题:Nginx会将请求分流把session数据保存在服务器内存就会造成,第一次访问在192.168.1.2产生session数据,再次请求可能会被分流到192.168.1.3 导致获取不到session数据。

可以设置nginx不同区域访问同一服务器列入河北IP访问192.168.1.2。上海IP访问192.168.1.3。
或者将session存储在Redis服务器中。

# coding:utf-8

from flask import Flask, session, current_app


app = Flask(__name__)

# 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


if __name__ == '__main__':
    app.run(debug=True)


请求上下文与应用上下文

在这里插入图片描述

请求上下文(request context)
request和session都属于请求上下文对象。

应用上下文(application context)
current_app和g都属于应用上下文对象。

在这里插入图片描述

current_app:表示当前运行程序文件的程序实例。
g:处理请求时,用于临时存储的对象,每次请求都会重设这个变量。
方便函数之间的传参。

请求钩子

钩子就是一个预留占位的地方,方便代码调用时一同去调用的。
请求钩子是通过装饰器的形式实现,Flask支持如下四种请求钩子:

before_first_request:在处理第一个请求前运行。

@app.before_first_request

before_request:在每次请求前运行。

after_request(response):如果没有未处理的异常抛出,在每次请求后运行。

teardown_request(response):在每次请求后运行,即使有未处理的异常抛出。

# coding:utf-8

from flask import Flask, session, current_app, request, url_for


app = Flask(__name__)


@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"):
    # if path in [url_for("index"),url_for("hello")]:
        print("在请求钩子中判断请求的视图逻辑: index")
    elif path == url_for("hello"):
        print("在请求钩子中判断请求的视图逻辑: hello")
    print("handle_teardown_request 被执行")
    return response


if __name__ == '__main__':
    app.run()


在这里插入图片描述

Flask-Script扩展命令行

pip install Flask-Script

# coding:utf-8

from flask import Flask
from flask_script import Manager   # 启动命令的管理类


app = Flask(__name__)

# 创建Manager管理类的对象
manager = Manager(app)


@app.route("/index")
def index():
    return "index page"


if __name__ == '__main__':
    # app.run(debug=True)
    # 通过管理对象来启动flask
    manager.run()

启动命令

python  xx.py runserver
python  xx.py runserver -h 0.0.0.0 -p 6000 # 指定Ip和端口号
python  xx.py shell  # 直接操作
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值