当前位置:网站首页>Flask - 中间件
Flask - 中间件
2022-04-23 05:47:00 【峰爷520】
其实就是封装旧酒,装进新瓶,自己再加点料。留坑,还没有用到。
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的实现原理?看见都是这样用第三方扩展的。日后研究
版权声明
本文为[峰爷520]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_41752427/article/details/111592047
边栏推荐
- Rust: Tcp 服务器与客户端的一个简单例子
- [leetcode 59] spiral matrix II
- 6.Reversal
- 日志
- Cf515b drazil and his happy friends
- [transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
- Record the installation and configuration of gestermer on TX2, and then use GST RTSP server
- 11.a==b?
- scikit-learn sklearn 0.18 官方文档中文版
- Option的正确打开方式
猜你喜欢
从源代码到可执行文件的过程
[leetcode 401] binary Watch
[leetcode 54] spiral matrix
A solution to replace not in in SQL
1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition
scikit-learn sklearn 0.18 官方文档中文版
C language file operation
Integration and induction of knowledge points of automatic control principle (Han min version)
RPC must know and know
GDAL+OGR学习
随机推荐
2. Average length of words
Understanding and installing MySQL
Use of multithreaded executors
9.Life, the Universe, and Everything
Cf1427c the hard work of paparazzi
Gesture recognition research
深拷贝和浅拷贝的区别
[leetcode217] there are duplicate elements
The problem that the page will refresh automatically after clicking the submit button on the form is solved
Formation à la programmation
Definition of C class and method
Techniques et principes de détection
Rust 中的 RefCell
用二进制进行权限管理
斯坦福机器学习课程汇总
Robocode教程4——Robocode的游戏物理
GDAL+OGR学习
12. Monkeys climb mountains
Problems and solutions of database migration
serde - rust的序列化方案