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との両方ではなく、getどうすればチェックインできpostますか?

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 に答える