[英]How to preprocess all calls?
I use bottle.route()
to redirect HTTP queries to appropriate functions 我使用
bottle.route()
将HTTP查询重定向到适当的功能
import bottle
def hello():
return "hello"
def world():
return "world"
bottle.route('/hello', 'GET', hello)
bottle.route('/world', 'GET', world)
bottle.run()
I would like to add some preprocessing to each call, namely the capacity to act upon the source IP (obtained via bottle.request.remote_addr
). 我想为每个调用添加一些预处理,即对源IP进行操作的能力(通过
bottle.request.remote_addr
获得)。 I can specify the preprocessing in each route 我可以在每条路线中指定预处理
import bottle
def hello():
preprocessing()
return "hello"
def world():
preprocessing()
return "world"
def preprocessing():
print("preprocessing {ip}".format(ip=bottle.request.remote_addr))
bottle.route('/hello', 'GET', hello)
bottle.route('/world', 'GET', world)
bottle.run()
but this looks awkward. 但这看起来很尴尬。
Is there a way to plug the preprocessing function on a global level? 有没有办法在全局级别上插入预处理功能? (so that each call goes though it?)
(以便每个电话都能通过吗?)
I think you can use bottle's Plugin 我认为您可以使用Bottle的插件
doc here: http://bottlepy.org/docs/dev/plugindev.html#bottle.Plugin 此处的文档: http : //bottlepy.org/docs/dev/plugindev.html#bottle.Plugin
code example 代码示例
import bottle
def preprocessing(func):
def inner_func(*args, **kwargs):
print("preprocessing {ip}".format(ip=bottle.request.remote_addr))
return func(*args, **kwargs)
return inner_func
bottle.install(preprocessing)
def hello():
return "hello"
def world():
return "world"
bottle.route('/hello', 'GET', hello)
bottle.route('/world', 'GET', world)
bottle.run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.