Flask基础【集合】

一、Flask简介

1 Flask 和pythonweb框架介绍

# python web框架 ,本质都一样
	-django:大而全,内置的app多,第三方app也多
    -Flask:小而精,没有过多的内置组件,只完成web框架最基本的功能,需要借助于第三方,完成更丰富的功能
    -web.py:是一个小巧灵活的Python框架,它简单而且功能强大(国内几乎没有用的)
    -------异步web框架------
    -fastapi:python的异步web框架,不少公司再用,https://fastapi.tiangolo.com/zh/
    -sanic:python的异步web框架,供支持异步高并发请求的 web 服务
    -tornado:异步框架,用的比较少了

# 同步框架和异步框架的区别
	-djagno是同步框架还是异步框架,djagno 3.x以后支持异步
    -同步框架的意思:一个线程只处理一个请求
    -异步框架的意思:一个线程可以处理多个请求
    -异步框架可以很显著的提高并发量

image-20230331092959206

1.1 flask介绍

Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架
	-jinja2 模板语法,django的dtl,非常像
    -Werkzeug WSGI 符合wsgi协议的web服务器,django使用的是wsgiref
    
    
    
#### wsgirf写web
from wsgiref.simple_server import make_server

# mya 就等同于django
def mya(environ, start_response):
    #把environ包装成了request
    print(environ)
    start_response('200 OK', [('Content-Type', 'text/html')])
    if environ.get('PATH_INFO') == '/index':
        with open('index.html','rb') as f:
            data=f.read()

    elif environ.get('PATH_INFO') == '/login':
        with open('login.html', 'rb') as f:
            data = f.read()
    else:
        data=b'<h1>Hello, web!</h1>'
    return [data]  # 做成了response

if __name__ == '__main__':
    myserver = make_server('', 8008, mya)
    print('监听8010')
    myserver.serve_forever()

#### 使用werkzeug写web
from werkzeug.wrappers import Request, Response


@Request.application
def hello(request):
    return Response('Hello World!')


if __name__ == '__main__':
    from werkzeug.serving import run_simple

    run_simple('localhost', 4000, hello)

2 flask快速使用

# 安装:pip install flask   安装依赖: MarkupSafe, Werkzeug, Jinja2, flask
	-1.x 没有本质区别
    -2.x 没有本质区别,源码上动了,用起来一样
from flask import Flask

app = Flask(__name__)


# app=Flask('test')


# 注册路由---->装饰器
@app.route('/index')
def index():
    return 'hello web'


@app.route('/')
def home():
    return 'hello home'


if __name__ == '__main__':
    # app.run('127.0.0.1',5000)
    app.run()

3 登录,显示用户信息小案例

3.1 login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form method="post">
    <p>用户名:<input type="text" name="username"></p>
    <p>密码:<input type="password" name="password"></p>
    <input type="submit" value="登录"> {{error}}
</form>
</body>
</html>

3.2 home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>用户列表</h1>
<table>
    {% for k,v in user_dict.items() %}
    <tr>
        <td>{{k}}</td>
        <td>{{v.name}}</td>
        <td>{{v['name']}}</td>
        <td>{{v.get('name')}}</td>
        <td><a href="/detail/{{k}}">查看详细</a></td>
    </tr>
    {% endfor %}
</table>
</body>
</html>

3.3 detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>名字是:{{user.name}}</p>
<p>年龄是:{{user['age']}}</p>
<p>性别是:{{user.get('gender')}}</p>
<p>{{user.text}}</p>


</body>
</html>

3.4 py文件

from flask import Flask, request, render_template, redirect, session,jsonify

app = Flask(__name__)

# 要使用session,必须设置秘钥,秘钥是配置信息
app.secret_key = 'asdfasdfa33aef3aefads'

USERS = {
    1:{'name':'张三','age':18,'gender':'男','text':"道路千万条"},
    2:{'name':'李四','age':28,'gender':'男','text':"安全第一条"},
    3:{'name':'王五','age':18,'gender':'女','text':"行车不规范"},
}
# 1 创建templates文件夹,写login.html
@app.route('/login', methods=['GET', 'POST'])
def index():
    # 没有request对象,使用全局的request
    # get请求,返回模板
    if request.method == 'GET':
        return render_template('login.html')  # 新手四件套之一:返回模板
    else:
        # post请求,校验数据
        # 取出前端传入的用户名密码,校验
        username = request.form.get('username')  # 等同于django的的request.POST
        password = request.form.get('password')
        if username == 'lqz' and password == '123':
            # 登录成功,保存登录状态 重定向到跟路径   新手四件套之一:重定向
            # 保存到session中,session是全局的
            session['name'] = username
            return redirect('/')
        else:
            return render_template('login.html', error='用户名或密码错误')  # 注意跟django的render区分,要模板渲染的数据,直接key=value传即可


@app.route('/')
def home():
    # 校验,登录成功,才能过来,不登录,重定向到登录页面
    if session.get('name'):  # 有值说明登录了,没有值说明没有登录
        return render_template('home.html',user_dict=USERS)
    else:
        return redirect('/login')


@app.route('/detail/<int:pk>')
def detail(pk):
    if session.get('name'):  # 有值说明登录了,没有值说明没有登录
        user_detail = USERS.get(pk)
        return render_template('detail.html', user=user_detail)
    else:
        return redirect('/login')


@app.route('/test')
def test():
    return jsonify([{'name':'lqz','age':19}])
if __name__ == '__main__':
    app.run()



'''
# 学到的
    1 注册路由  app.route(路径,methods=[请求方式get,post])
    2 新手四件套:
        -render_template   渲染模板 跟django有区别
        -redirect  重定向
        -return 字符串 返回字符串
        -jsonify 返回json格式
        
    3 请求的request对象,是全局的,直接导入使用即可,在不同视图函数中不会混乱
        request.method  请求方式
        request.form   post请求的body体的内容转成了字典
        
    4 session 全局的,直接导入使用即可,一定要指定秘钥app.secret_key = 'asdfasdfa33aef3aefads'
        放值:session['name']='lqz'
        取值:session.get('name')
        
    5 模板的渲染
        -兼容django的dtl
        -更强大,可以加括号,字典可以.get  .values()   .items()
        -{% for %}
    
    6 转换器@app.route('/detail/<int:pk>')

'''

4 配置文件方式

# django 有个settings
# flask 也有配置问题,但是它的使用方式多种:
	# 设置的方式一:(测试用)
    # app.debug=True  # 调试模式,提示信息更详细,修改代码不需要重启,自动重启
    # app.secret_key='dasdfasdfasd'  # 秘钥,只能 放debug和secret_key

    ## 设置方式二:直接使用app.config设置
    # app.config['DEBUG']=True
    # app.config['SECRET_KEY']='sdfasdfasd'
    # print(app.config)


    ## 方式三:使用py文件(不常用)
    # app.config.from_pyfile("settings.py")
    # print(app.config)

    ## 方式四:常用的,使用类的方式
    # app.config.from_object('settings.DevelopmentConfig')
    # app.config.from_object('settings.ProductionConfig')
    # print(app.config)

    ### 其他:
    #通过环境变量配置
    # app.config.from_envvar("环境变量名称")

    # json
    # app.config.from_json("json文件名称")
    # JSON文件名称,必须是json格式,因为内部会执行json.loads


    # 字典格式---》配置中心
    # app.config.from_mapping({'DEBUG': True})
   



# 内置的配置字段,其他可以写自己的,比如 redis的连接地址,mysql的连接地址
	-DEBUG
    -SECRET_KEY
    -SESSION_COOKIE_NAME
    -PERMANENT_SESSION_LIFETIME

二、路由系统

1 路由本质

# django中配置路由 在urls.py 中,写path ,写在 列表中
# flask是基于装饰器的,大部分都用装饰器来做,少量可以抽取到一个urls.py种




# 路由的装饰器源码分析
	# 咱们这样写
    @app.route('/login')
    def index():
        pass
    
    #本质是---》index=app.route('/login')(index)
    
    # app.route('/login')的执行结果 decorator 函数
    	-rule是路径
        -其他参数都给了options
    # 然后 decorator(index)--->在执行
    		# f是index
    		endpoint = options.pop("endpoint", None) # 目前没有endpoint,是None
            # 核心,本质--》self就是实例化得到的app对象,flask对象
            # app对象中有个方法add_url_rule,这是在添加路由
            # 不使用装饰器,自己注册路由
            self.add_url_rule(rule, endpoint, f, **options)
            return f
        
        
    def route(self, rule: str, **options: t.Any) -> t.Callable[[T_route], T_route]:
        def decorator(f: T_route) -> T_route:
            endpoint = options.pop("endpoint", None)
            self.add_url_rule(rule, endpoint, f, **options)
            return f

        return decorator
# 可以不使用装饰器的方式,注册路由
	app.add_url_rule('/', endpoint=None, view_func=home, methods=['GET'])
    
    
# flask路由的本质是app对象的add_url_rule完成路由的注册

2 路由参数add_url_rule

# rule             URL规则
# view_func        视图函数名称
# defaults = None  默认值, 当URL中无参数,函数需要参数时,使用defaults = {'k': 'v'}为函数提供参数
# endpoint = None, 路径的别名,名称,用于反向解析URL,即: url_for('名称')
# methods = None, 允许的请求方式,如:["GET", "POST"]


#对URL最后的 / 符号是否严格要求
strict_slashes = None
    '''
        @app.route('/index', strict_slashes=False)
        #访问http://www.xx.com/index/ 或http://www.xx.com/index均可
        @app.route('/index', strict_slashes=True)
        #仅访问http://www.xx.com/index
    '''

#重定向到指定地址
redirect_to = None, 
    '''
        @app.route('/index/<int:nid>', redirect_to='/home/<nid>')
    '''
    
    
 # 需要记住的
    # rule  
    # view_func   
    # defaults
    # endpoint
    # methods

3 转换器

 'default':          UnicodeConverter,
 'string':           UnicodeConverter,
 'any':              AnyConverter,
 'path':             PathConverter,
 'int':              IntegerConverter,
 'float':            FloatConverter,
 'uuid':             UUIDConverter,
    
 # 了解:让路由支持正则(忽略掉)

三、flask视图

1 cbv分析

# 基于类的视图,写法

from flask import Flask,request
from flask.views import View, MethodView

app = Flask(__name__)

app.debug = True


# 视图类,继承MethodView,类中写跟请求方式同名的方法即可,之前学的所有都一致
class IndexView(MethodView):
    def get(self):
        print(request.method)
        return 'get 请求'

    def post(self):
        print(request.method)
        return 'post 请求'


app.add_url_rule('/index', endpoint='index', view_func=IndexView.as_view('index'))
if __name__ == '__main__':
    app.run()

2 源码分析

# 1 IndexView.as_view('index') 执行完的结果,是个函数(view的)内存地址
    def as_view(cls, name, *class_args, **class_kwargs):
        def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
            # 本质是在执行self.dispatch_request,只是用了异步
           return current_app.ensure_sync(self.dispatch_request)(**kwargs)
        return view
    
    
# 2 请求来了,执行view()--->本质在执行self.dispatch_request---》MethodView中的
    def dispatch_request(self, **kwargs):
        # self是视图类的对象
        meth = getattr(self, request.method.lower(), None)
        # 用异步执行meth()
        return current_app.ensure_sync(meth)(**kwargs)
    
    
# 3 总结:执行原理跟django一样


# 4 路径如果不传别名,别名就是函数名---》分析一下源码
	-@app.route('/index')--》没有传endpoint
    -endpoint 就是None---》调用了app.add_url_rule,传入了None
    if endpoint is None:
       endpoint = _endpoint_from_view_func(view_func)  # type: ignore
	-_endpoint_from_view_func 就是返回函数的名字
# 5 as_view('index') 必须传参数,传进来的参数是,是【别名】
	# view是as_view内的内层函数,闭包函数
	view.__name__ = name  # 修改了函数的名字变成了你传入的
    
    # app.add_url_rule('/index',view_func=IndexView.as_view('index'))
    简写成:app.add_url_rule('/index',view_func=view)
    #如果不传参数, 所有人的别名(endpoint),都是内层函数view,所以就报错了
    
    
   

#6 补充:flask的路由注册使用装饰器,如果写了一个登录认证装饰器,那么应该放在路由装饰器上还是下?
	-放在路由下面
    -路由必须传endpoint,如果不传,又报错

    
    
#7  视图类必须继承MethodView,如果继承View,它的dispatch_request没有具体实现,你的视图类必须重写dispatch_request,我们不想重写,继承MethodView
    def dispatch_request(self) -> ft.ResponseReturnValue:
        raise NotImplementedError()
    
    
# 8 视图类加装饰器,直接配置在类属性上【decorators】即可
	decorators = [auth,]
    # 源码,cls是视图类,中有decorators
    if cls.decorators:
      for decorator in cls.decorators:
          view = decorator(view)  # view=auth(view)

# 源码学到的
	-1 as_view 执行流程跟djagno一样
    -2 路径如果不传别名,别名就是函数名(endpoint)
    -3 视图函数加多个装饰器(上下顺序和必须传endpoint)
    -4 视图类必须继承MethodView,否则需要重写dispatch_request
    -5 视图类加装饰器:类属性decorators = [auth,]
    

image-20230403102112301

四、模板

1 py

from flask import Flask, render_template,Markup

app = Flask(__name__, template_folder='templates', static_folder='static')  # 模板的路径必须是templates,因为实例化app对象时,传入的
app.debug=True


def add(a,b):
    return a+b
@app.route('/')
def index():
    a='<a href="http://www.baidu.com">点我看美女</a>'  # 不存在xss攻击,处理了xss
    a=Markup(a)
    return render_template('index.html',name='lqz',a=a,add=add)


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

2 html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>模板语法,static</h1>
<img src="/static/1.jpg" alt="">

<h1>模板语法,if</h1>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello World!</h1>
{% endif %}

<h1>模板语法,标签渲染</h1>
{{a|safe}}
{{a}}

<h1>模板语法,执行函数</h1>
{{add(4,5)}}

</body>
</html>

五、请求与响应

# 请求:全局的request对象

# 响应:四件套
from flask import Flask, request, make_response,render_template

app = Flask(__name__)
app.debug = True


@app.route('/', methods=['GET', 'POST'])
def index():
    #### 请求
    # request.method  提交的方法
    # request.args  get请求提及的数据
    # request.form   post请求提交的数据
    # request.values  post和get提交的数据总和
    # request.cookies  客户端所带的cookie
    # request.headers  请求头
    # request.path     不带域名,请求路径
    # request.full_path  不带域名,带参数的请求路径
    # request.script_root
    # request.url           带域名带参数的请求路径
    # request.base_url    带域名请求路径
    # request.url_root      域名
    # request.host_url    域名
    # request.host       127.0.0.1:500
    print(request.method)
    print(request.args)
    print(request.form)
    print(request.values)
    print(request.cookies)
    print(request.headers)
    print(request.path)
    print(request.full_path)
    print(request.url)
    print(request.base_url)
    print(request.host_url)
    print(request.host)

    obj = request.files['file']
    obj.save(obj.filename)

    ### 响应  四件套
    # 1 响应中写入cookie
    # response = 'hello'
    # res = make_response(response)  # flask.wrappers.Response
    # print(type(res))
    # res.set_cookie('xx','xx')
    # return res
    # 2 响应头中写数据(新手四件套,都用make_response包一下)
    response = render_template('index.html')
    res = make_response(response)  # flask.wrappers.Response
    print(type(res))
    res.headers['yy']='yy'
    return res


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

六、session及源码分析

1 session的使用

from flask import Flask, request, session, render_template, redirect

app = Flask(__name__)
app.debug = True
app.secret_key = 'asdfas33asdfasf'


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    else:
        name = request.form.get('name')
        password = request.form.get('password')
        print(password)
        session['name'] = name
        return redirect('/index')


@app.route('/index', methods=['GET', 'POST'])
def index():
    return 'hello %s' % session.get('name', '匿名用户')

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

2 源码分析

# cookie :存在于客户端浏览器的键值对
# session:存在于服务端的键值对    # djagno 放在了django_session表中

# flask中,叫session,问题来了,存哪里了?
	-加密后,放到了cookie中,如果session发生了变化,我们的cookie也会跟着变
    

    
# 源码部分:
	# 1 app.session_interface  配置了一个类的对象,这个就是session的执行流程
    # 2 类中有两个非常重要的方法,请求来了,会执行open_session,请求走了会执行save_session
      def open_session(self, app, request) :
		#1 根据名字,取出前端传入的cookie的value值
        val = request.cookies.get(self.get_cookie_name(app))
        #2 如果没有val,构造了一个空session对象
        if not val:
            return self.session_class()
        max_age = int(app.permanent_session_lifetime.total_seconds())
        try:
            # 如果没有过期,解码,做成session对象,后续直接用session即可
            data = s.loads(val, max_age=max_age)
            return self.session_class(data)
        except BadSignature:
            # 如果过期了,也是空session
            return self.session_class()
        
        


    def save_session(self, app, session, response) :
        name = self.get_cookie_name(app)
		# 取出过期事件,和把session加密转成字符串,放到cookie中
        expires = self.get_expiration_time(app, session)
        val = self.get_signing_serializer(app).dumps(dict(session))
        response.set_cookie(
            name,
            val, 
            expires=expires,
        )
        
        
        
        
  # 扩展,想把session放到redis中,mysql中,已经有人帮咱们写了,第三方的
	只需要写个类,重写open_session,save_session自己写

3 session

cookie:存放在客户端的键值对
session:存放在客户端的键值对
token:存放在客户端,通过算法来校验

在使用session之前必须现在设置一下密钥

app.secret_key="asdas" #值随便

除请求对象之外,还有一个 session 对象。它允许你在不同请求间存储特定用户的信息。它是在 Cookies 的基础上实现的,并且对 Cookies 进行密钥签名要使用会话,你需要设置一个密钥。 (app.session_interface对象)

设置:session['username'] = 'xxx'
#在django中发什么三件事,1,生成一个随机的字符串 2 往数据库存 3 写入cookie返回浏览器
#在flask中他没有数据库,但session是怎样实现的?
	# 生成一个密钥写入这个cookie,然后下次请求的时候,通过这个cookie解密,然后赋值给session
    #我们通过app.session_interface来查看
  

删除:session.pop('username', None)

app.session_interface中save_session的参数(设置cookie的参数)

key, 键
value='', 值
max_age=None, 超时时间 cookie需要延续的时间(以秒为单位)如果参数是\ None`` ,这个cookie会延续到浏览器关闭为止
expires=None, 超时时间(IE requires expires, so set it if hasn't been already.)
path='/', Cookie生效的路径,/ 表示根路径,特殊的:根路径的cookie可以被任何url的页面访问,浏览器只会把cookie回传给带有该路径的页面,这样可以避免将cookie传给站点中的其他的应用。
domain=None, Cookie生效的域名 你可用这个参数来构造一个跨站cookie。如, domain=".example.com"所构造的cookie对下面这些站点都是可读的:www.example.com 、 www2.example.com 和an.other.sub.domain.example.com 。如果该参数设置为 None ,cookie只能由设置它的站点读取
secure=False, 浏览器将通过HTTPS来回传cookie
httponly=False 只能http协议传输,无法被JavaScript获取(不是绝对,底层抓包可以获取到也可以被覆盖)

session源码的执行流程

-save_seesion
	-响应的时候,把session中的值加密序列化放大到了cookie中,返回到浏览器中
-open_session
	-请求来了,从cookie中取出值,反解,生成session对象,以后再视图函数中直接用sessoin就可以了。

七、闪现

# flash 翻译过来的
	- 当次请求先把一些数据,放在某个位置
    - 下一次请求,把这些数据取出来,取完,就没了
    
# 作用:
	1 可以跨请求,来保存数据
    2 当次请求,访问出错,被重定向到其他地址,重定向到这个地址后,拿到当时的错误
    
    
# djagno中有这个东西吗?
	-message框架
# 用法:
	-设置 闪现  
    	-flash('%s,我错了'%name) ,可以设置多次,放到列表中
        -flash('超时错误',category="debug") 分类存 
    -获取 闪现
    	-get_flashed_messages() ,取完就删除
        -get_flashed_messages(category_filter=['debug'])分类取
        
        
        
 # 本质,放到session中

八、请求扩展

# 请求扩展中:在请求来了,或请求走了,可以绑定一些函数,到这里就会执行这个函数,类似于django的中间件
# 在flask中就用请求扩展,来代替djagno的中间件

# 好几个请求扩展
	-before_request:请求来了会走,如果他返回了四件套,就结束了
    -after_request :请求走了会走,一定要返回response对象
    -before_first_request:第一次来了会走
    -teardown_request:无论是否出异常,会走
    -errorhandler:监听状态码,404  500
    -template_global:标签
    -template_filter:过滤器
from flask import Flask, request,render_template

app = Flask(__name__)


####1 before_request 和 after_request
# 请求来了,执行一个函数,来的时候从上往下执行
# @app.before_request
# def before():
#     print('我来了111')
#     # if 'index' in request.path:
#     return '不让看了'  # 如果不是retrun了None,说明被拦截了,直接返回
#
#
# @app.before_request
# def before1():
#     print('我来了222')
#
#
# # 请求走了,执行一个函数,走的时候,从下往上执行
# @app.after_request
# def after(response):
#     print('我走了111')
#     return response
#
#
# @app.after_request
# def after2(response):
#     print('我走了222')
#     return response


# 2 项目启动后的第一个请求
# @app.before_first_request
# def first():
#     print('我的第一次')


# 3 teardown_request,无论视图函数是否出错,都会执行它,做错误日志
# @app.teardown_request
# def teardown(e):
#     print(e)
#     print('执行我了')



# 4 errorhandler  监听响应状态码,如果符合监听的状态码,就会走它
# @app.errorhandler(404)
# def error_404(arg):
#     return "404错误了"

# @app.errorhandler(500)
# def error_500(arg):
#     return "500错误了"


##5 template_global  在模板中直接使用该过滤器
@app.template_global()
def add(a1, a2):
    return a1 + a2

# 6 template_filter
@app.template_filter()
def db(a1, a2, a3):
    return a1 + a2 + a3


@app.route('/')
def index():
    # a = [1, 2, 3]
    # print(a[9])
    return render_template('index1.html')


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

九、蓝图

1.1 蓝图介绍

buleprint 蓝图:之前在一个py中写flask项目,后期需要划分项目

1.2 flask项目目录演变过程

(1) 单文件

from flask import Flask

# 1 生成app对象
app = Flask(__name__)


# 2 写视图函数,并配置路由
@app.route('/user')
def user():
    return 'user page'


@app.route('/order')
def order():
    return 'order page'


if __name__ == '__main__':
    # 3 运行项目
    app.run('127.0.0.1', 9999)

(2) 没有蓝图的flask项目

image-20230404152459958
flask项目        
	-src             #核心源码位置
		-__init__.py   # 包 里面实例化得到了app对象,
    -models.py     #放表模型
    -views.py      # 放视图函数
	-static          # 放静态资源
  	-xxx.png
	-templates       # 放模板
		-home.html     # 模板
	-manage.py       # 启动文件

src - __init__.py

生成app实例

# 生成app实例
from flask import Flask

app = Flask(__name__, template_folder='../template', static_folder='../static')

# flask项目配置
# app.config.from_file()
app.debug = True
app.secret_key = 'aaaa'

# 重点,不然项目运行不起来
from . import views

但是此时 flask项目并不能运行起来,因为此时视图函数并没有加载,需要导入到包中,才能运行

src - views.py

# 项目的视图函数

from . import app
from flask import render_template

# 视图函数
@app.route('/home')
def home():
    return render_template('home.html')

manage.py

项目的运行文件

from src import app

if __name__ == '__main__':
    app.run('127.0.0.1',8999)

(3) 小型-有蓝图的flask项目

image-20230404155702472

① 第1步:导入蓝图类

② 第2步:实例化得到蓝图对象

src - views - shop.py

from flask import Blueprint, render_template

# 1 生成蓝图实例
shop_bp = Blueprint('shop', __name__)


# 蓝图也有自己的请求扩展
@shop_bp.before_request
def shop_before_request():
    print('让会议倒带')
    

# 2 编写视图函数
@shop_bp.route('/shop')
def shop_index():
    return render_template('shop_index.html')

③ 第3步:在app中注册蓝图

src - __init__.py

# 生成app
from flask import Flask

app = Flask(__name__, template_folder='./templates/', static_folder='./static/')

# 配置
# app.config.from_pyfile()
app.debug = True

# 在__init__.py中需要注册
from .views.user import user_bp
from .views.shop import shop_bp

app.register_blueprint(user_bp)
app.register_blueprint(shop_bp)

④ 第4步:在不同的views.py种使用蓝图注册路由

@shop_bp.route('/shop')

  • 注意
    • 使用蓝图时,蓝图可以有自己的静态文件和模版
    • 注册蓝图时,可以使用前缀,必须以/ 开头

(4) 大型-有蓝图的flask项目

使用蓝图,划分大型项目目录 多个app,像django一样

image-20230404164528527

项目的__init__.py中注册蓝图

from flask import Flask

app = Flask(__name__)

app.config.from_pyfile('settings.py')

# 注册蓝图
from .admin import admin_bp
from .shop import shop_bp
from .user import user_bp

app.register_blueprint(admin_bp, url_prefix='/admin')
app.register_blueprint(shop_bp, url_prefix='/shop')
app.register_blueprint(user_bp, url_prefix='/user')

1.3 蓝图的使用步骤

① 第1步:导入蓝图类

② 第2步:实例化得到蓝图对象

③ 第3步:在app中注册蓝图

④ 第4步:在不同的views.py种使用蓝图注册路由

2 g对象

2.1 简介

由于global的缩写,在python中是一个关键字。

所以在flask框架中,使用g对象,可以在整个请求的全局可以放值可以取值,相当于其他框架中的context上下文

  • 优点:是全局的可以在任意位置导入使用即可,不用request的原因是怕数据污染,request中有很多内置的变量

  • g对象和session的区别:g只是针对当次请求,而session针对的是多次请求

2.2 实例

from . import admin_bp
from flask import render_template, g


@admin_bp.before_request
def admin_before_request():
    g.message = '这是g对象中的信息' # 在g对象中添加消息
    print('请求扩展')


def accept_msg(a, b):

    return a+b

@admin_bp.route('/home')
def home():
    print('这是g', g)  # 这是g <flask.g of 'src'>
    print('这是g.message', g.message)  # 这是g.message 这是g对象中的信息
    res = accept_msg(1, 100)
    print(res)
    return render_template('home.html')

3 数据库连接池

3.1 flask操作pymql

① 全局连接对象

在全局中创建一个连接对象,把连接对象做成全局的,在视图函数中,使用全局的连接,查询返回给前端

缺点:会出现数据错乱

image-20230404190224088

    conn = pymysql.connect(
        user='root',
        password="",
        host='127.0.0.1',
        database='bbs001',
        port=3306,
    )
  # 在全局中使用

② 创建多个对象

每个来的请求都创建一个连接,请求结束后关闭连接(django就是这样的)

缺点:虽然数据库系统允许的最大可连接数max_connections。这个参数是可以设置的。如果不设置,默认是100。最大是16384。

但是,因为创建和销毁数据库的连接,都会消耗系统的资源,所以为了避免在同一时间同时打开过多的连接线程,现在编程一般都使用所谓数据库连接池技术。

@app.route('/test')
def test():
    # 1.pymysql连接mysql,生成连接对象
    conn = pymysql.connect(
        user='root',
        password="",
        host='127.0.0.1',
        database='bbs001',
        port=3306,
    )
    # 2.生成游标对象
    cursor = conn.cursor()  # 生成的是元组套元组
    # cursor = conn.cursor(pymysql.cursors.DictCursor) # 生成的是列表套字典
    # 3.编写sql语句
    sql = 'select * from app01_blog;'
    cursor.execute(sql)
    res = cursor.fetchall()
    print(res)
    return jsonify(res)

3.2 使用第三方数据库连接池

① 安装dbutils

pip install dbutils

② 使用:实例化生成池对象


from dbutils.pooled_db import PooledDB
import pymysql
pool = PooledDB(
    creator=pymysql,  # 使用链接数据库的模块
    maxconnections=10,  # 连接池允许的最大连接数,0和None表示不限制连接数
    mincached=2,  # 初始化时,链接池中至少创建的空闲的链接,0表示不创建
    maxcached=5,  # 链接池中最多闲置的链接,0和None不限制
    maxshared=3,
    # 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。
    blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
    maxusage=None,  # 一个链接最多被重复使用的次数,None表示无限制
    setsession=[],  # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."]
    ping=0,
    # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
    host='',
    port=,
    user='',
    password='',
    database='',
    charset='utf8'
)

③ 在视图函数中导入使用

def test():
    # 1.创建连接对象
    conn = pool.connection()
    # 2.生成游标对象
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    time.sleep(random.randint(1,4))
    # 3.执行sql语句
    sql = 'select id,site_name,site_title,site_theme from app01_blog;'
    cursor.execute(sql)
    res = cursor.fetchall()
    print(res)
    return jsonify(res)

3.3 压力测试代码

from threading import Thread
import requests


def task():
    res = requests.get('http://127.0.0.1:7777/')
    print(len(res.text))


if __name__ == '__main__':
    for i in range(1000):
        t = Thread(target=task)
        t.start()
  • 不使用连接池的连接数大
image-20230404193741265
  • 使用连接池的连接数小
image-20230404194236362

十、请求上下文分析(源码:request原理)

1 导出项目的依赖 - pipreqs模块

使用步骤

① 安装pipreqs模块

pip3 install pipreqs

② 导出

在终端的项目目录下执行

pipreqs ./
  • mac和linux会正常

  • 在mac中遇到了zsh: command not found: pipreqs的错误,这时候切换到项目目录下,运行如下命令就可以导出requirements.txt文件了

python3 -m  pipreqs.pipreqs
  • win可能会因为编码问题会报错
pipreqs ./ --encoding=utf8

2 函数和方法

(1)函数和方法的区别

有自动传值才是方法,不然就是普通函数

① 函数:就是普通的函数,有几个参数就会传几个参数

② 方法:绑定给对象的方法,绑定给类的方法,绑定给谁,由谁来调用,会自动将自身传入

(2)绑定给类的方法 和 绑定给对象的方法

  • 类的绑定方法:会自动把类传入,对象可以调用,
  • 对象的绑定方法,类可以来调用:但是就变成普通函数了,需要几个值就要传几个值
@classmethod:绑定给类的方法 绑定给对象的方法:实例方法 @staticmethod:静态方法
对象是否可以调用 所有对象都可以调用,并将【类cls】自动传入 对象可以调用,并将【对象自己】自动传入,self该参数会在方法被调用时自动传递当前对象的引用 普通函数,需要手动传值
类是否可以调用 类可以调用 不能通过类调用,该方法变为普通函数,需要手动传值 普通函数,需要手动传值
  • MethodType判断是否是类的方法, FunctionType
rom types import MethodType, FunctionType


class Foo(object):
    def fetch(self):
        pass

    def say_hello(self, word):
        print(word)

    @classmethod
    def test(cls, a):
        print(cls)
        print(a)

    @staticmethod
    def test1():
        pass




obj = Foo()
obj.test(1)
Foo.say_hello(obj,'nihao')
# obj.say_hello = lambda :print('hello')

# 1 类调用对象的方法
print(isinstance(Foo, MethodType))  #False 调用对象的绑定方法,该方法就变成了普通函数,需要手动传值

# 2 对象调用对象的方法
print(isinstance(obj.say_hello,MethodType))  # True

# 3 对象调用类的方法
print(isinstance(obj.test, MethodType))  # True

# 4 类调用类的方法
print(isinstance(Foo.test,MethodType))  # True

3 threading.local对象

(1)介绍

在 Python 的 threading 模块中,有一个 local 对象,它提供了一种线程本地存储的机制。线程本地存储是指每个线程都有自己的数据存储空间,不同线程之间的数据互不干扰。local 对象可以在多线程环境中用来存储每个线程的数据,

由于并发编程的时候,多个线程操作同一个变量,会出现并发安全的问题,需要加锁

但是使用local对象,多线程并发操作时,不需要加锁,不会出现数据错乱问题,

其他语言中也有这个东西ThreadLocal,java中面试会被经常问到

(2)本质原理

多个线程修改同一个数据,复制多份变量给每个线程用,为每个线程开辟一块空间进行数据存储
每个线程操作自己的那部分数据

① 不同线程中的数据错乱

当task函数在阻塞了2s后,全局变量common_data已经为9,所以此时所有的线程中的数据 common_data都产生了数据错乱

"1 不同线程中的数据错乱"
from threading import Thread, get_ident
import time

common_data = 99


def task(arg):
    global common_data

    common_data = arg

    time.sleep(2)
    print('线程号:%s' % get_ident(), common_data)


for i in range(10):
    t = Thread(target=task, args=(i,))
    t.start()

② 使用互斥锁:解决并发的数据安全问题

通过使用互斥锁,这样数据就不会发送数据错乱问题,但是将并发变成了串行,这样效率大大降低

from threading import Thread, get_ident, Lock
import time

common_data = 99

data_lock = Lock()


def task(arg):

    # 给代码块上锁,防止数据混乱
    data_lock.acquire()
    global common_data
    common_data = arg
    time.sleep(2)
    print('线程号:%s' % get_ident(), common_data)
    data_lock.release()


for i in range(10):
    t = Thread(target=task, args=(i,))
    t.start()

③ 使用local对象

使用local对象,不会出现并发安全问题,并且不需要将并发变为串行

from threading import Thread, get_ident,local
import time

# 1 生成local对象
common_data = local()

def task(arg):
    global common_data
    # 2 给local对象添加属性
    common_data.arg = arg

    time.sleep(2)
    print('线程号:%s' % get_ident(), common_data.arg)


for i in range(10):
    # 3 修改local对象,每个线程互相不会错乱
    t = Thread(target=task, args=(i,))
    t.start()

④ flask中的local对象:request

flask中的request就是使用local,但是local对象只能区分线程,不能区分协程。所以flask还将其兼容线程和协程

try:
    from greenlet import getcurrent as get_ident
except Exception as e:
    from threading import get_ident

from threading import Thread
import time


class Local(object):
    def __init__(self):
        object.__setattr__(self, 'storage', {})  # 这个不会触发setattr
        # self.storage={}  # 它会触发 __setattr__,就会递归

    def __setattr__(self, k, v):
        ident = get_ident() #如果用协程,这就是协程号,如果是线程,这就是线程号
        if ident in self.storage:  #{'协程id号':{arg:1},'协程id号':{arg:2},'协程id号':{arg:3}}
            self.storage[ident][k] = v
        else:
            self.storage[ident] = {k: v}

    def __getattr__(self, k):
        ident = get_ident()
        return self.storage[ident][k]


lqz = Local()
def task(arg):
    lqz.arg = arg
    print(lqz.arg)


for i in range(10):
    t = Thread(target=task, args=(i,))
    t.start()

4 偏函数

# 可以提前传值


from functools import partial
def add(a,b,c):
    return a+b+c


# print(add(2,3,4))  # 传少了报错

# 现在只有一个参数,后面的俩参数,需要过一会才知道
# 借助于偏函数,先提前给他把第一个参数传入,后面知道了后面俩参数,再传后面俩

add=partial(add,2)
#
# # 干了很多事
#
print(add(3,4))

十一、flask 整个生命执行流程 1.1.4版本

(1)flask生命周期的执行流程

① 应用程序初始化

当 Flask 应用程序启动时,它会执行一次初始化,包括创建 Flask 实例、加载配置、注册插件等。

② 请求处理

请求来了

app.run()结果就是执行app加括号--> app()--> Flask类产生对象,则执行 Flask.__call__方法--> self.wsgi_app(environ, start_response)

  • self.wsgi_app(environ, start_response)

    其结果是将environ, start_response传给了Flask类中的wsgi_app方法,

def __call__(self, environ: dict, start_response: t.Callable) -> t.Any:
    # environ是讲http请求拆分成了字典
    
    ...
    return self.wsgi_app(environ, start_response)
  • self.wsgi_app(environ, start_response)

    根据请求 URL 和 HTTP 方法,找到对应的视图函数

def wsgi_app(self, environ: dict, start_response: t.Callable) -> t.Any:
    # ctx对象: RequestContext(self, environ)由RequestContext产生,对象里有当次请求的requests对象,app对象,session对象
    
    ctx = self.request_context(environ)
    error: t.Optional[BaseException] = None
    try:
        try:
            # RequestContext类中的push方法
            
            ctx.push()
            
            # 匹配路由成功后,执行视图函数
            # 根据请求 URL 和 HTTP 方法,找到对应的视图函数
            response = self.full_dispatch_request()
        except Exception as e:
            error = e
            response = self.handle_exception(e)
        except:  # noqa: B001
            error = sys.exc_info()[1]
            raise
        return response(environ, start_response)
    finally:
        if "werkzeug.debug.preserve_context" in environ:
            environ["werkzeug.debug.preserve_context"](_cv_app.get())
            environ["werkzeug.debug.preserve_context"](_cv_request.get())

        if error is not None and self.should_ignore_error(error):
            error = None

        ctx.pop(error)
  • ctx.push() --> RequestContext类中的push()方法
 def push(self):
		# _request_ctx_stack = LocalStack() ---》push(ctx对象)--》ctx:request,session,app
        _request_ctx_stack.push(self)
		#session相关的
        if self.session is None:
            session_interface = self.app.session_interface
            self.session = session_interface.open_session(self.app, self.request)

            if self.session is None:
                self.session = session_interface.make_null_session(self.app)
		# 路由匹配相关的
        if self.url_adapter is not None:
            self.match_request()
  • LocalStack()中的push()方法
    def push(self, obj):
        #self._local  _local 就是咱们刚刚自己写的Local的对象---》LocalStack的init初始化的_local---》self._local = Local()---》Local对象可以根据线程协程区分数据 
        rv = getattr(self._local, "stack", None)
        # 一开始没有值
        if rv is None:
            rv = []
            self._local.stack = rv  # self._local.stack 根据不同线程用的是自己的数据
        rv.append(obj)  # self._local.stack.append(obj)
        # {'线程id号':{stack:[ctx]},'线程id号2':{stack:[ctx]}}
        return rv
      
 # 再往后执行,就会进入到路由匹配,执行视图函数
	# request = LocalProxy(partial(_lookup_req_object, "request"))
    # LocalProxy 代理类---》method---》代理类去当前线程的stack取出ctx,取出当时放进去的request
	视图函数中:print(request.method)
    
    
# print(request) 执行LocalProxy类的__str__方法
# request.method 执行LocalProxy类的__getattr__
    def __getattr__(self, name): #name 是method
        # self._get_current_object() 就是当次请求的request
        return getattr(self._get_current_object(), name)
  • LocalProxy类的方法_get_current_object
 # LocalProxy类的方法_get_current_object
   def _get_current_object(self):
        if not hasattr(self.__local, "__release_local__"):
            return self.__local()
        try:
            return getattr(self.__local, self.__name__)
        except AttributeError:
            raise RuntimeError("no object bound to %s" % self.__name__)
            
            
            
 # self.__local 是在 LocalProxy 类实例化的时候传入的local

# 在这里实例化的:request = LocalProxy(partial(_lookup_req_object, "request"))
# local 是 partial(_lookup_req_object, "request")

#_lookup_req_object ,name=request
def _lookup_req_object(name):
    top = _request_ctx_stack.top  # 取出了ctx,是当前线程的ctx
    if top is None:
        raise RuntimeError(_request_ctx_err_msg)
    return getattr(top, name)  #从ctx中反射出request,当次请求的request

(2)总结

请求上下文执行流程(ctx):

请求上下文执行流程(ctx):
		-0 flask项目一启动,有6个全局变量
			-_request_ctx_stack:LocalStack对象
			-_app_ctx_stack :LocalStack对象
			-request : LocalProxy对象
			-session : LocalProxy对象
		-1 请求来了 app.__call__()---->内部执行:self.wsgi_app(environ, start_response)
		-2 wsgi_app()
			-2.1 执行:ctx = self.request_context(environ):返回一个RequestContext对象,并且封装了request(当次请求的request对象),session,flash,当前app对象
			-2.2 执行: ctx.push():RequestContext对象的push方法
				-2.2.1 push方法中中间位置有:_request_ctx_stack.push(self),self是ctx对象
				-2.2.2 去_request_ctx_stack对象的类中找push方法(LocalStack中找push方法)
				-2.2.3 push方法源码:
				    def push(self, obj):
						#通过反射找self._local,在init实例化的时候生成的:self._local = Local()
						#Local(),flask封装的支持线程和协程的local对象
						# 一开始取不到stack,返回None
						rv = getattr(self._local, "stack", None)
						if rv is None:
							#走到这,self._local.stack=[],rv=self._local.stack
							self._local.stack = rv = []
						# 把ctx放到了列表中
						#self._local={'线程id1':{'stack':[ctx,]},'线程id2':{'stack':[ctx,]},'线程id3':{'stack':[ctx,]}}
						rv.append(obj)
						return rv
		-3 如果在视图函数中使用request对象,比如:print(request)
			-3.1 会调用request对象的__str__方法,request类是:LocalProxy
			-3.2 LocalProxy中的__str__方法:lambda x: str(x._get_current_object())
				-3.2.1 内部执行self._get_current_object()
				-3.2.2 _get_current_object()方法的源码如下:
				    def _get_current_object(self):
						if not hasattr(self.__local, "__release_local__"):
							#self.__local()  在init的时候,实例化的,在init中:object.__setattr__(self, "_LocalProxy__local", local)
							# 用了隐藏属性
							#self.__local 实例化该类的时候传入的local(偏函数的内存地址:partial(_lookup_req_object, "request"))
							#加括号返回,就会执行偏函数,也就是执行_lookup_req_object,不需要传参数了
							#这个地方的返回值就是request对象(当此请求的request,没有乱)
							return self.__local()
						try:
							return getattr(self.__local, self.__name__)
						except AttributeError:
							raise RuntimeError("no object bound to %s" % self.__name__)
				-3.2.3 _lookup_req_object函数源码如下:
					def _lookup_req_object(name):
						#name是'request'字符串
						#top方法是把第二步中放入的ctx取出来,因为都在一个线程内,当前取到的就是当次请求的ctx对象
						top = _request_ctx_stack.top
						if top is None:
							raise RuntimeError(_request_ctx_err_msg)
						#通过反射,去ctx中把request对象返回
						return getattr(top, name)
				-3.2.4 所以:print(request) 实质上是在打印当此请求的request对象的__str__
		-4 如果在视图函数中使用request对象,比如:print(request.method):实质上是取到当次请求的reuquest对象的method属性
		
		-5 最终,请求结束执行: ctx.auto_pop(error),把ctx移除掉
		
	其他的东西:
		-session:
			-请求来了opensession
				-ctx.push()---->也就是RequestContext类的push方法的最后的地方:
					if self.session is None:
						#self是ctx,ctx中有个app就是flask对象,   self.app.session_interface也就是它:SecureCookieSessionInterface()
						session_interface = self.app.session_interface
						self.session = session_interface.open_session(self.app, self.request)
						if self.session is None:
							#经过上面还是None的话,生成了个空session
							self.session = session_interface.make_null_session(self.app)
			-请求走了savesession
				-response = self.full_dispatch_request() 方法内部:执行了before_first_request,before_request,视图函数,after_request,savesession
				-self.full_dispatch_request()---->执行:self.finalize_request(rv)-----》self.process_response(response)----》最后:self.session_interface.save_session(self, ctx.session, response)
		-请求扩展相关
			before_first_request,before_request,after_request依次执行
		-flask有一个请求上下文,一个应用上下文
			-ctx:
				-是:RequestContext对象:封装了request和session
				-调用了:_request_ctx_stack.push(self)就是把:ctx放到了那个位置
			-app_ctx:
				-是:AppContext(self) 对象:封装了当前的app和g
				-调用 _app_ctx_stack.push(self) 就是把:app_ctx放到了那个位置
	-g是个什么鬼?
		专门用来存储用户信息的g对象,g的全称的为global 
		g对象在一次请求中的所有的代码的地方,都是可以使用的 
		
		
	-代理模式
		-request和session就是代理对象,用的就是代理模式

5 wtforms(了解)form组件

# django 有forms组件
	- 生成前端模板
    - 校验数据
    - 渲染错误信息
 
# flask 中使用第三方的wtforms 实现像django的forms一样的功能
	- 第一步:导入,定义一个类,继承forms
    -第二步:模板中, for循环生成模板
    -第三步:视图函数中,使用form校验数据


    
# py代码
from flask import Flask, render_template, request, redirect
from wtforms import Form
from wtforms.fields import simple
from wtforms import validators
from wtforms import widgets

app = Flask(__name__, template_folder='templates')

app.debug = True


class LoginForm(Form):
    # 字段(内部包含正则表达式)
    name = simple.StringField(
        label='用户名',
        validators=[
            validators.DataRequired(message='用户名不能为空.'),
            validators.Length(min=6, max=18, message='用户名长度必须大于%(min)d且小于%(max)d')
        ],
        widget=widgets.TextInput(), # 页面上显示的插件
        render_kw={'class': 'form-control'}

    )
    # 字段(内部包含正则表达式)
    pwd = simple.PasswordField(
        label='密码',
        validators=[
            validators.DataRequired(message='密码不能为空.'),
            validators.Length(min=8, message='用户名长度必须大于%(min)d'),
            validators.Regexp(regex="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}",
                              message='密码至少8个字符,至少1个大写字母,1个小写字母,1个数字和1个特殊字符')

        ],
        widget=widgets.PasswordInput(),
        render_kw={'class': 'form-control'}
    )



@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        form = LoginForm()
        return render_template('login.html', form=form)
    else:
        form = LoginForm(formdata=request.form)
        if form.validate():
            print('用户提交数据通过格式验证,提交的值为:', form.data)
        else:
            print(form.errors)
        return render_template('login.html', form=form)

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

# html代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>登录</h1>
<form method="post" novalidate>
    <p>{{form.name.label}}: {{form.name}} {{form.name.errors[0] }}</p>

    <p>{{form.pwd.label}} {{form.pwd}} {{form.pwd.errors[0] }}</p>
    <input type="submit" value="提交">
</form>
</body>
</html>

十二、信号

1 Flask中的信号

信号:信号是一种事件处理机制,可以让应用程序中的不同部分之间进行通信

Flask框架中的信号基于blinker(安装这个模块),其主要就是让开发者可是在flask请求过程中定制一些用户行为 flask 和django都有

pip3 install blinker

使用信号的这种模式:观察者模式,又叫发布-订阅(Publish/Subscribe) 23 种设计模式之一

2 Flask中的内置信号

用户表新增一条记录,就记录一下日志

	 -方案一:在每个增加后,都写一行代码  ---》后期要删除,比较麻烦
   -方案二:使用信号,写一个函数,绑定内置信号,只要程序执行到这,就会执行这个函数
① Flask中内置信号
request_started = _signals.signal('request-started')                # 请求到来前执行
request_finished = _signals.signal('request-finished')              # 请求结束后执行
 
before_render_template = _signals.signal('before-render-template')  # 模板渲染前执行
template_rendered = _signals.signal('template-rendered')            # 模板渲染后执行
 
got_request_exception = _signals.signal('got-request-exception')    # 请求执行出现异常时执行
 
request_tearing_down = _signals.signal('request-tearing-down')      # 请求执行完毕后自动执行(无论成功与否)
appcontext_tearing_down = _signals.signal('appcontext-tearing-down')# 应用上下文执行完毕后自动执行(无论成功与否)
 
appcontext_pushed = _signals.signal('appcontext-pushed')            # 应用上下文push时执行
appcontext_popped = _signals.signal('appcontext-popped')            # 应用上下文pop时执行
message_flashed = _signals.signal('message-flashed')                # 调用flask在其中添加数据时,自动触发
② 内置信号使用步骤:

第1步:写一个函数,由于信号触发时,会自动传入一些参数,所以该函数的参数最好写成 *args, **kwargs
第2步:绑定内置信号
第3步:等待被触发

from flask import Flask, render_template, before_render_template
# 导入内置信号

app = Flask(__name__)
app.debug = True


# 写一个函数,由于信号触发时,会自动传入一些参数,所以该函数的参数最好写成 *args, **kwargs

def signal_func(*args, **kwargs):
    print('args---', args)
    print('kwargs---', kwargs)
    print('执行了')

# 将函数绑定给内置信号
before_render_template.connect(signal_func)


@app.route('/')
def home():
    return render_template('home.html', name='duoduo')


if __name__ == '__main__':
    app.run('127.0.0.1', 7788)
    
------------------------------------------------

args--- (<Flask '01 信号'>,)
kwargs--- {'template': <Template 'home.html'>, 'context': {'name': 'duoduo', 'g': <flask.g of '01 信号'>, 'request': <Request 'http://127.0.0.1:7788/' [GET]>, 'session': <NullSession {}>}}
执行了

③ 定义信号使用步骤:

第1步:自定义一个信号

导入from flask.signals import _signals, 自定义信号xxx=_signals.signal('xxxx')

第2步:写一个函数,由于是自定义的函数,所以要传入什么参数需要我们定义

第3步:将函数绑定给自定义的信号

第4步:编写函数触发的位置

from flask import Flask, render_template
from flask.signals import _signals

app = Flask(__name__)
app.debug = True
from datetime import datetime

# 1 自定义信号
my_signal = _signals.signal('xxx')


# 2 写一个函数
def task(time):

    print('现在的时间为%s' % time)


# 3 绑定自定义的信号
my_signal.connect(task)


@app.route('/')
def home():
    my_signal.send(datetime.now())
    return render_template('home.html', name='duoduo')


if __name__ == '__main__':
    app.run('127.0.0.1', 9977)

3 Django中的信号

(1) django的内置信号

Model signals
    pre_init                    # django的modal执行其构造方法前,自动触发
    post_init                   # django的modal执行其构造方法后,自动触发
    pre_save                    # django的modal对象保存前,自动触发
    post_save                   # django的modal对象保存后,自动触发
    pre_delete                  # django的modal对象删除前,自动触发
    post_delete                 # django的modal对象删除后,自动触发
    m2m_changed                 # django的modal中使用m2m字段操作第三张表(add,remove,clear)前后,自动触发
    class_prepared              # 程序启动时,检测已注册的app中modal类,对于每一个类,自动触发
Management signals
    pre_migrate                 # 执行migrate命令前,自动触发
    post_migrate                # 执行migrate命令后,自动触发
Request/response signals
    request_started             # 请求到来前,自动触发
    request_finished            # 请求结束后,自动触发
    got_request_exception       # 请求异常后,自动触发
Database Wrappers
    connection_created          # 创建数据库连接时,自动触发

(2)django中使用内置信号

第1步:写一个函数,由于信号触发时,会自动传入一些参数,所以该函数的参数最好写成 *args, **kwargs
第2步:绑定内置信号
第3步:等待被触发

1 写一个函数
def callBack(*args, **kwargs):
    print(args)
    print(kwargs)
2 绑定信号
#方式一
post_save.connect(callBack)
# 方式二
from django.db.models.signals import pre_save
from django.dispatch import receiver
@receiver(pre_save)
def my_callback(sender, **kwargs):
    print("对象创建成功")
    print(sender)
    print(kwargs)
3 等待触发

十三、flask-script:命令行模块

1 简介

Flask-Script是一个用于编写命令行脚本的扩展,可以在Flask应用程序中定义和运行命令行命令

2 使用

① 安装

pip3 install flask-script

② 修改代码

run.py文件中

# 1 导入自定义命令模块
from flask_script import Manager
from myapp import app

# 2 生成命令对象
manager = Manager(app)

if __name__ == '__main__':
    # 3 运行该脚本,run方法 
    manager.run()

③ 用命令启动

在项目目录下

python run.py runserver

3 自定义命令

① 自定制简单命令

@manager.command

# 导入自定义命令模块
from flask_script import Manager
from myapp import app

# 1 生成命令对象
manager = Manager(app)


# 2 定义 命令行命令
@manager.command
def my_command(a, b):
    # 命令行参数的逻辑
    c = int(a) + int(b)
    print(c)
    print('执行了')


if __name__ == '__main__':
    manager.run()
  • 终端中使用
python3 run.py my_command 3 4
------输出结果----
7
执行了

② 复杂命令

@manager.option

# 3 定义复杂命令
@manager.option('-i','--init',dest='init')
@manager.option('-c','--cmd',dest='cmd')
def com_command(init,cmd):
    # python3 run.py com_command -i aaa -c bbb
    # python3 run.py com_command --init ddd --cmd cccc

    print(init,cmd)
posted @ 2023-05-04 17:23  Duosg  阅读(33)  评论(0编辑  收藏  举报