如何重置 Bottle 中每个请求的自定义请求属性?
How to reset custom request attribute on each request in Bottle?
这是我的插件的简化版本:
class MyPlugin(object):
''' My plugin.
'''
api = 2
def __init__(self):
self.time = datetime.now()
def apply(self, callback, route):
request.my_attr = self.time
@wraps(callback)
def wrapper(*args, **kwargs):
cb_response = callback(*args, **kwargs)
return cb_response
# Forget any cached values.
route.reset()
# Replace the route callback with the wrapped one.
return wrapper
my_plugin = MyPlugin()
@route('/my_route', method=['GET', 'POST'], apply=[my_plugin])
def my_route():
""" Http Handler.
"""
response.content_type = 'text/txt;'
time = request.my_attr
print(time)
run(host=HOST, port=PORT, debug=DEBUG, quiet=QUIET)
它适用于第一个请求,但对所有后续请求都失败:
# ./my_app.py
Bottle v0.12.9 server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8888/
Hit Ctrl-C to quit.
2016-10-25 14:51:29.975600
46.101.xx.yy - - [25/Oct/2016 14:51:33] "POST /my_route HTTP/1.1" 200 1569
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/bottle.py", line 1391, in __getattr__
var = self.environ['bottle.request.ext.%s'%name]
KeyError: 'bottle.request.ext.my_attr'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/usr/local/lib/python3.4/dist-packages/bottle.py", line 1732, in wrapper
rv = callback(*a, **ka)
File "./my_app.py", line 245, in wrapper
cb_response = callback(*args, **kwargs)
File "./my_app.py", line 264, in my_route
time = request.my_attr
File "/usr/local/lib/python3.4/dist-packages/bottle.py", line 1394, in __getattr__
raise AttributeError('Attribute %r not defined.' % name)
AttributeError: Attribute 'my_attr' not defined.
46.101.xx.yy - - [25/Oct/2016 14:51:37] "POST /my_route HTTP/1.1" 500 760
如何在每次请求时重置 request.my_attr
?
不清楚您的最终目标是什么,或者您所说的 "reset request.my_attr on each request" 的确切含义(因为您在其他地方提到了 "cached values,",这意味着您 做 打算做一些缓存)。但这一行似乎很清楚
request.my_attr = self.time
放错地方了。你可能想把它移到你的 wrapper
函数中,因为(大概)然后意图是在每个请求上将该行设为 运行:
@wraps(callback)
def wrapper(*args, **kwargs):
request.my_attr = self.time
cb_response = callback(*args, **kwargs)
return cb_response
我不确定这对我是否有意义,但我不知道您的目标是什么,所以也许这确实是您的意图。在任何情况下,将行移动到 wrapper
都会更正 HTTP 500。希望对您有所帮助!如果没有,请添加一两行描述您要完成的任务,我会尽力提供帮助。
这是我的插件的简化版本:
class MyPlugin(object):
''' My plugin.
'''
api = 2
def __init__(self):
self.time = datetime.now()
def apply(self, callback, route):
request.my_attr = self.time
@wraps(callback)
def wrapper(*args, **kwargs):
cb_response = callback(*args, **kwargs)
return cb_response
# Forget any cached values.
route.reset()
# Replace the route callback with the wrapped one.
return wrapper
my_plugin = MyPlugin()
@route('/my_route', method=['GET', 'POST'], apply=[my_plugin])
def my_route():
""" Http Handler.
"""
response.content_type = 'text/txt;'
time = request.my_attr
print(time)
run(host=HOST, port=PORT, debug=DEBUG, quiet=QUIET)
它适用于第一个请求,但对所有后续请求都失败:
# ./my_app.py
Bottle v0.12.9 server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8888/
Hit Ctrl-C to quit.
2016-10-25 14:51:29.975600
46.101.xx.yy - - [25/Oct/2016 14:51:33] "POST /my_route HTTP/1.1" 200 1569
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/bottle.py", line 1391, in __getattr__
var = self.environ['bottle.request.ext.%s'%name]
KeyError: 'bottle.request.ext.my_attr'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/usr/local/lib/python3.4/dist-packages/bottle.py", line 1732, in wrapper
rv = callback(*a, **ka)
File "./my_app.py", line 245, in wrapper
cb_response = callback(*args, **kwargs)
File "./my_app.py", line 264, in my_route
time = request.my_attr
File "/usr/local/lib/python3.4/dist-packages/bottle.py", line 1394, in __getattr__
raise AttributeError('Attribute %r not defined.' % name)
AttributeError: Attribute 'my_attr' not defined.
46.101.xx.yy - - [25/Oct/2016 14:51:37] "POST /my_route HTTP/1.1" 500 760
如何在每次请求时重置 request.my_attr
?
不清楚您的最终目标是什么,或者您所说的 "reset request.my_attr on each request" 的确切含义(因为您在其他地方提到了 "cached values,",这意味着您 做 打算做一些缓存)。但这一行似乎很清楚
request.my_attr = self.time
放错地方了。你可能想把它移到你的 wrapper
函数中,因为(大概)然后意图是在每个请求上将该行设为 运行:
@wraps(callback)
def wrapper(*args, **kwargs):
request.my_attr = self.time
cb_response = callback(*args, **kwargs)
return cb_response
我不确定这对我是否有意义,但我不知道您的目标是什么,所以也许这确实是您的意图。在任何情况下,将行移动到 wrapper
都会更正 HTTP 500。希望对您有所帮助!如果没有,请添加一两行描述您要完成的任务,我会尽力提供帮助。