如何做url路径检查龙卷风RequestHandler的准备方法?

时间:2012-08-06 09:22:23

标签: python tornado

我想像龙卷风一样进行路径检查:

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中查看,而不是getpost

1 个答案:

答案 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 - 这就足够了。