Python 预请求处理
我正在寻找一种方法,使所有请求在进入路由之前都进入函数Python 预请求处理,python,uwsgi,bottle,Python,Uwsgi,Bottle,我正在寻找一种方法,使所有请求在进入路由之前都进入函数foo() 这样我就可以在做真正的工作之前阅读request.environ 我正试图这样做,这样我就不会重复代码,但在BottlyPy中找不到一种方法来做这样的事情 我的设置是:nginx->uwsgi->bottlepy。这就是它们的用途 下面是一个例子: import bottle from bottle import request, response def foo(callback): def wrapper(*args
foo()
这样我就可以在做真正的工作之前阅读request.environ
我正试图这样做,这样我就不会重复代码,但在BottlyPy中找不到一种方法来做这样的事情
我的设置是:nginx->uwsgi->bottlepy。这就是它们的用途
下面是一个例子:
import bottle
from bottle import request, response
def foo(callback):
def wrapper(*args, **kwargs):
# before view function execution
print(request.environ) # do whatever you want
body = callback(*args, **kwargs) # this line basically means "call the view normally"
# after view function execution
response.headers['X-Foo'] = 'Bar' # you don't need this, just an example
return body # another 'mandatory' line: return what the view returned (you can change it too)
return wrapper
bottle.install(foo)
这就是它们的用途
下面是一个例子:
import bottle
from bottle import request, response
def foo(callback):
def wrapper(*args, **kwargs):
# before view function execution
print(request.environ) # do whatever you want
body = callback(*args, **kwargs) # this line basically means "call the view normally"
# after view function execution
response.headers['X-Foo'] = 'Bar' # you don't need this, just an example
return body # another 'mandatory' line: return what the view returned (you can change it too)
return wrapper
bottle.install(foo)
你的意思是我应该写我自己的插件只是为了初始化一些我需要阅读request.environ的东西吗?没错。制作瓶子插件并不难。太棒了,非常感谢你提供的非常简单的代码示例。你是说我应该编写自己的插件来初始化一些我需要阅读请求的东西。环境?没错。制作瓶子插件并不难。太棒了,非常感谢您提供的非常简单的代码示例。