Flask框架
Flask框架的介绍
1.Flask是什么? - Flask是一个基于Python开发并且依赖jinjia2模板和Werkzeug WSGI服务的一个微型框架 - jinjia2: 是一个模板语法,和django的dtl非常像 - Werkzeug WSGI: 符合wsgi协议的web服务器,django使用wsgiref 2.案例: 使用wsgiref写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('监听8008') myserver.serve_forever() 3.案例2:使用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)
Flask快速使用
# 1.安装flask pip install flask # 2.flask安装依赖: MarkupSafe, Werkzeug, Jinja2, flask # 3.版本问题: -1.x 没有本质区别 -2.x 没有本质区别,源码上动了,用起来一样 # 4.代码案例 from flask import Flask app = Flask(__name__) # app = Flask('test') 指定app名字叫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()
使用Flask写一个显示用户登录信息的小案例
# 1.app.py from flask import Flask,request,render_template,redirect,session,jsonify app = Flask(__name__) # 要使用session,必须设置秘钥,秘钥是配置信息 app.secret_key = 'asdsfgdghghgjjtyjtyjtj' USERS = { 1: {'name': '张三', 'age': 18, 'gender': '男', 'text': "道路千万条"}, 2: {'name': '李四', 'age': 28, 'gender': '男', 'text': "安全第一条"}, 3: {'name': '王五', 'age': 18, 'gender': '女', 'text': "行车不规范"}, } # 创建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()
# 2.detail.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <p>名字:{{ user.name }}</p> <p>年龄:{{ user['age'] }}</p> <p>性别:{{ user.get('gender') }}</p> <p>{{ user.text }}</p> <body> </body> </html> # 3.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> 4.login.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <form method="post"> <p>用户名:<input type="text" name="username"></p> <p>密码:<input type="password" name="password"></p> <input type="submit" value="登录">{{ error }} </form> <body> </body> </html>
Flask知识点
''' # 学到的 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>') '''
Flask的配置文件
# 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
Flask路由
### 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, # 了解:让路由支持正则(忽略掉)
# 基于类的视图,写法 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() # 源码分析 # 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,]
模板
# 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() # 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以及源码分析
# 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() # 源码分析 # 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自己写
执行原理
闪现
# 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()
蓝图使用
# blueprint 翻译过来的,称之为蓝图 # 作用是:之前全在一个py中写flask项目,后期肯定要划分目录 # 不用蓝图,划分目录 no_blueprint_flask # 项目名 src #核心源码位置 __init__.py # 包 里面实例化得到了app对象, models.py #放表模型 views.py # 放视图函数 static # 放静态资源 templates # 放模板 home.html # 模板 manage.py # 启动文件 # 蓝图的使用步骤 -第一步:导入蓝图类 from flask import Blueprint -第二步:实例化得到蓝图对象 us=Blueprint('user',__name__) -第三步:在app中注册蓝图 app.register_blueprint(us) -第四步:在不同的views.py 使用蓝图注册路由 @us.route('/login') -补充:蓝图可以有自己的静态文件和模板 -补充:注册蓝图时,可以使用前缀,必须以/ 开头 # 使用蓝图,划分小型项目目录 little_blueprint # 项目名 -src # 核心代码 -static # 静态文件 -1.jpg # 图片 -templates # 模板文件 -user.html # 模板 -views # 视图函数存放位置 -order.py # 订单相关视图 -user.py # 用户相关视图 -__init__.py # 包 -models.py # 表模型 -manage.py # 启动文件 # 使用蓝图,划分大型项目目录 多个app,像django一样 big_blueprint # 项目名 -src # 核心文件 -admin # admin的app -static # 静态文件 -1.jpg # 图片 -templates # 模板文件目录 -admin_home.html # 模板文件 -__init__.py # 包 -models.py # 表模型 -views.py # 视图函数 -home # home app -order # orderapp -__init__.py # 包 -settings.py # 配置文件 -manage.py # 启动文件
g对象
# g 对象 是什么? -global的缩写,再python中是个关键字,不能以关键字作为变量名,干脆用了g -g 对象,在整个请求的全局,可以放值,可以取值 -全局变量,在任意位置导入使用即可 -它为什么不学django使用request作为上下文? -因为使用request,可能会造成request数据的污染,不小心改了request的属性,但你不知道 -建议使用g 是空的,放入之后在当次请求中全局优先 # 以后想在当次请求中,放入一些数据,后面使用,就可以使用g对象 # g和session有什么区别? -g 是只针对于当次请求 -session针对于多次请求 #### 代码实例 from flask import Flask, g, request app = Flask(__name__) app.debug = True @app.before_request def before(): if 'home' in request.path: g.xx = 'xx' def add(a, b): # print('---',g.name) print('---', request.name) return a + b @app.route('/') def index(): print(g.xx) name = request.args.get('name') # g.name = name request.method = name res = add(1, 2) print(res) return 'index' @app.route('/home') def home(): print(g.xx) return 'index' if __name__ == '__main__': app.run()
数据库连接池
# flask 操作mysql -使用pymysql -在视图函数中,创建pymysql的连接,查数据,查完,返回给前端 -有什么问题? 来一个请求,创建一个连接,请求结束,连接关闭 (djanog就是这么做的) -把连接对象,做成全局的,在视图函数中,使用全局的连接,查询,返回给前端 -有什么问题?会出现数据错乱,详见下图 # 解决上面的两个问题 -数据库连接池 -创建一个全局的池 -每次进入视图函数,从池中取一个连接使用,使用完放回到池中,只要控制池的大小,就能控制mysql连接数 # 使用第三方数据库连接池,使用步骤 -1 安装 pip install dbutils -2 使用:实例化得到一个池对象 -3 在视图函数中导入使用 conn = pool.connection() cursor = conn.cursor(pymysql.cursors.DictCursor) cursor.execute('select id,title,author_img from aritcle limit 2') res = cursor.fetchall() # 带池的代码 @app.route('/article_pool') def article_pool(): conn = pool.connection() cursor = conn.cursor(pymysql.cursors.DictCursor) cursor.execute('select id,title,author_img from aritcle limit 2') res = cursor.fetchall() print(res) return jsonify(res) # 不带池的代码 @app.route('/article') def article(): conn = pymysql.connect(user='root', password="", host='127.0.0.1', database='cnblogs', port=3306) cursor = conn.cursor(pymysql.cursors.DictCursor) time.sleep(random.randint(1,3)) cursor.execute('select id,title,author_img from aritcle limit 2') res = cursor.fetchall() cursor.close() conn.close() return jsonify(res) # 压力测试代码 from threading import Thread import requests def task(): res = requests.get('http://127.0.0.1:5000/article_pool') print(len(res.text)) if __name__ == '__main__': for i in range(500): t = Thread(target=task) t.start() ## 效果是: 使用池的连接数明显小 不使用池连接数明显很大 # 查看数据库连接数 show status like 'Threads%'
请求上下文分析
### 1.导出项目依赖 # 之前 pip freeze >requirments.txt 把当前解释器环境下的所有第三方依赖都导出来 # 使用第三方模块,更精确的导出依赖 pipreqs 第一步:安装 pip3 install pipreqs 第二步:使用命令,导出项目依赖 pipreqs ./ -win由于编码问题会出错:pipreqs ./ --encoding=utf8 -mac,linx没有问题 第三步:就会在项目根路径下生成:requirements.txt ### 2.函数和方法 # 只要会自动传值,就是方法,函数,有几个值就要传几个值,否则报错 # 函数就是普通的函数,有几个参数就要传几个参数 # 方法:绑定给对象的方法,绑定给类的方法,绑定给谁的,由谁来调用,会自动把自身传入 # 类的绑定方法,对象可以来调用,会自动把类传入 # 对象的绑定方法,类可以来调用? 类可以调用,但是它就变成了普通函数,有几个值,就要传几个值,没有自动传值了 # MethodType检查一个对象,是不是方法 # FunctionType检查一个对象,是不是函数 # isinstance 判断一个对象,是不是一个类的对象 # issubclass 判断一个类,是不是另一个类的子类 from types import MethodType, FunctionType class Foo(object): def fetch(self): pass @classmethod def test(cls): pass
@staticmethod def test1(): pass # a=Foo() # print(isinstance(a,Foo)) # print(isinstance('a',Foo))
# class Foo2(Foo): # pass # class Foo3(): # pass # print(issubclass(Foo2,Foo)) # print(issubclass(Foo3,Foo)) def add(): pass # 类来调用对象的绑定方法, print(isinstance(Foo.fetch, MethodType)) # False 类来调用对象的绑定方法,该方法就变成了普通函数 obj = Foo() print(isinstance(obj.fetch, MethodType)) # True 对象来调用自己的绑定方法,fetch就是方法 print(isinstance(Foo.fetch, FunctionType)) # True 类来调用对象的绑定方法,该方法就变成了普通函数 print(isinstance(add, FunctionType)) # True 就是个普通函数 print(isinstance(add, MethodType)) # False 就是个普通函数 print(isinstance(Foo.test, MethodType)) # True test 是绑定给类的方法,类来调用,就是方法 print(isinstance(obj.test, MethodType)) # True 对象调用类的绑定方法,还是方法 print(isinstance(Foo.test1, MethodType)) # False 是普通函数 print(isinstance(obj.test1, MethodType)) # False 是普通函数 print(isinstance(obj.test1, FunctionType)) # True,静态方法,就是普通函数,对象和类都可以调用,有几个值就传几个值 ### 3.threading.local对象 # local 对象 # 并发编程时,多个线程操作同一个变量,会出现并发安全的问题,咱们需要加锁 # 使用local对象,多线程并发操作时,不需要加锁,不会出现数据错乱threading.local # 其他语言中也有这个东西ThreadLocal,java中面试会被经常问到,python没人问 # 本质原理: 多个线程修改同一个数据,复制多份变量给每个线程用,为每个线程开辟一块空间进行数据存储 每个线程操作自己的那部分数据 ### 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生命执行流程
# 请求来了---》app()----->Flask.__call__--->self.wsgi_app(environ, start_response) def wsgi_app(self, environ, start_response): # environ:http请求拆成了字典 # ctx对象:RequestContext类的对象,对象里有:当次的requets对象,app对象,session对象 ctx = self.request_context(environ) error = None try: try: #ctx RequestContext类 push方法 ctx.push() # 匹配成路由后,执行视图函数 response = self.full_dispatch_request() except Exception as e: error = e response = self.handle_exception(e) except: error = sys.exc_info()[1] raise return response(environ, start_response) finally: if self.should_ignore_error(error): error = None ctx.auto_pop(error) # RequestContext :ctx.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 --->obj 是ctx对象 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 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
请求上下文执行流程(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就是代理对象,用的就是代理模式
# 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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律