I want to do path check in tornado like this:
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),])
How can I check it in prepare
, not both get
and post
?
How can I check it in prepare, not both get and post?
Easy!
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
Here we think, that your action does not contain '/' in it's name. In other cases check will be different. By the way, you have access to request
in prepare method - that is enough.