4

我想像这样在龙卷风中进行路径检查:

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

4

1 回答 1

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

在这里,我们认为您的操作名称中不包含“/”。在其他情况下,检查会有所不同。顺便说一句,您可以使用requestprepare 方法——这就足够了。

于 2012-08-06T12:36:15.067 回答