我想像龙卷风一样进行路径检查:
class MyRequestHandler(tornado.web.RequestHandler):
def initialize(self):
self.supported_path = ['path_a', 'path_b', 'path_c']
def get(self, action):
if action not in self.supported_path:
self.send_error(400)
def post(self, action):
if action not in self.supported_path:
self.send_error(400)
# not implemented
#def prepare(self):
# if action match the path
app = tornado.web.Application([
('^/main/(P<action>[^\/]?)/', MyRequestHandler),])
如何在prepare
中查看,而不是get
和post
?
答案 0 :(得分:2)
我如何在准备中检查它,而不是获取和发布?
轻松!
class MyRequestHandler(tornado.web.RequestHandler):
def initialize(self):
self.supported_path = ['path_a', 'path_b', 'path_c']
def prepare(self):
action = self.request.path.split('/')[-1]
if action not in self.supported_path:
self.send_error(400)
def get(self, action):
#real code goes here
def post(self, action):
#real code goes here
我们认为,您的操作名称中不包含“/”。在其他情况下,检查将有所不同。顺便说一句,您可以在prepare方法中访问request
- 这就足够了。