對一個web應用來說中間件的作用真的非常大,請求前處理一下,請求后處理一下,出錯了處理一下。
而bottle框架由于標準的wsgi框架,所以我們可以利用中間件制作中間功能。
下面給出代碼:
~~~
# coding:UTF-8
from bottle import Bottle, run
app = Bottle()
@app.get('/')
def index():
return "輸出內容"
class Middle(object):
def __init__(self, obj):
self.app = obj
def __call__(self, environ, start_response):
print("請求前處理")
r = self.app(environ, start_response)
print("請求后處理")
return r
app = Middle(app)
run(app=app, host="127.0.0.1", port=8000, reloader=True, debug=True)
~~~