Tornado如何停止当前请求处理程序?
这里有一些子类继承自 ManageHandler
,每个子类都需要进行私有检查。于是,我写了一个 private_auth
方法,让它在 __init__
中进行私有检查,并在处理 GET/POST 方法之前调用它。
如果私有检查失败,就返回 404 错误。但是,这个方法没有奏效。那么,如何才能停止请求并返回错误页面呢?
我的代码如下:
class ManageHandler(BaseHandler):
def __init__(self, *argc, **argkw):
super(ManageHandler, self).__init__(*argc, **argkw)
self.private_auth()
def private_auth(self):
self.user = self.get_secure_cookie("user")
self.private = self.UserModel.get_user_level_by_name(self.user)
#not login
if self.private == -1:
return
if self.private != 4:
self.render("404.html")
self.finish()
@tornado.web.authenticated
def get(self, argkw={}):
pass
Tornado 报告了这个错误。
[E 140321 02:06:04 iostream:357] Uncaught exception, closing connection.
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 354, in wrapper
callback(*args)
File "/Library/Python/2.7/site-packages/tornado/stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "/Library/Python/2.7/site-packages/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 328, in _on_headers
self.request_callback(self._request)
File "/Library/Python/2.7/site-packages/tornado/web.py", line 1651, in __call__
handler = spec.handler_class(self, request, **spec.kwargs)
File "/Users/tyw/Workspaces/CMPUT391/trunk/app/handlers/ManageHandler.py", line 23, in __init__
self.private_auth()
File "/Users/tyw/Workspaces/CMPUT391/trunk/app/handlers/ManageHandler.py", line 36, in private_auth
self.finish()
File "/Library/Python/2.7/site-packages/tornado/web.py", line 837, in finish
self.flush(include_footers=True)
File "/Library/Python/2.7/site-packages/tornado/web.py", line 784, in flush
for transform in self._transforms:
TypeError: 'NoneType' object is not iterable
[E 140321 02:06:04 ioloop:491] Exception in callback <functools.partial object at 0x10c63e1b0>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/tornado/ioloop.py", line 477, in _run_callback
callback()
File "/Library/Python/2.7/site-packages/tornado/stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "/Library/Python/2.7/site-packages/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 354, in wrapper
callback(*args)
File "/Library/Python/2.7/site-packages/tornado/stack_context.py", line 331, in wrapped
raise_exc_info(exc)
File "/Library/Python/2.7/site-packages/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 328, in _on_headers
self.request_callback(self._request)
File "/Library/Python/2.7/site-packages/tornado/web.py", line 1651, in __call__
handler = spec.handler_class(self, request, **spec.kwargs)
File "/Users/tyw/Workspaces/CMPUT391/trunk/app/handlers/ManageHandler.py", line 23, in __init__
self.private_auth()
File "/Users/tyw/Workspaces/CMPUT391/trunk/app/handlers/ManageHandler.py", line 36, in private_auth
self.finish()
File "/Library/Python/2.7/site-packages/tornado/web.py", line 837, in finish
self.flush(include_footers=True)
File "/Library/Python/2.7/site-packages/tornado/web.py", line 784, in flush
for transform in self._transforms:
TypeError: 'NoneType' object is not iterable
1 个回答
2
Tornado这个框架并不是为了在RequestHandler.__init__
里处理身份验证和其他操作而设计的。这就是为什么当你在__init__
中调用self.finish
时会出现异常,因为此时RequestHandler还没有准备好去执行finish
。
相反,你应该重写get_current_user()
这个方法。基本的操作步骤可以在这里找到:
http://tornado.readthedocs.org/en/latest/web.html#tornado.web.RequestHandler.get_current_user
这里有一个示例:
http://technobeans.wordpress.com/2012/08/14/tornado-authentication/
在你的get_current_user()
方法中,不要设置self.user
和self.private
,只需返回一个元组。像这样:
def get_current_user(self):
private = -1
user = self.get_secure_cookie("user")
if user:
private = self.UserModel.get_user_level_by_name(self.user)
return (user, private) if private == 4 else None
Tornado会处理剩下的事情。在你的get()
方法中,当前的(user, private)
元组会被设置到self.current_user
中。