其实就是封装旧酒,装进新瓶,自己再加点料。留坑,还没有用到。
Flask的请求扩展就是Django的中间件。Django的中间件不是Flask的中间件
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello World!'
class Md(object):
def __init__(self,old_wsgi_app):
self.old_wsgi_app = old_wsgi_app
def __call__(self, environ, start_response):
print('开始之前')
# 。。。一些自定义操作
ret = self.old_wsgi_app(environ, start_response)
# 。。。一些自定义操作
print('结束之后')
return ret
if __name__ == '__main__':
# 封装旧的app.wsgi_app,这是app.run()的原理,看源码。会调用__call__方法
app.wsgi_app = Md(app.wsgi_app)
app.run()
from flask import Flask, flash, redirect, render_template, request
app = Flask(__name__)
app.secret_key = 'some_secret'
@app.route('/')
def index1():
return render_template('index.html')
@app.route('/set')
def index2():
v = request.args.get('p')
flash(v)
return 'ok'
class MiddleWare:
def __init__(self,wsgi_app):
self.wsgi_app = wsgi_app
def __call__(self, *args, **kwargs):
return self.wsgi_app(*args, **kwargs)
if __name__ == "__main__":
app.wsgi_app = MiddleWare(app.wsgi_app)
app.run(port=9999)
问题:这是不是Flask-extensions的实现原理?看见都是这样用第三方扩展的。日后研究