单个URL的几个请求处理程序

时间:2014-07-16 21:00:18

标签: python google-app-engine python-2.7 webapp2

将请求处理程序链接到URL的典型方法如下所示:

application = webapp2.WSGIApplication([('/', RequestHandler1)]

如果我想将多个请求处理程序链接到单个URL。这可能吗?我在想这样的事情:

application = webapp2.WSGIApplication([('/', (RequestHandler1, RequestHandler2)]

1 个答案:

答案 0 :(得分:0)

您可以执行正则表达式匹配以匹配网址参数

例如:

application = webapp2.WSGIApplication([
    ('/([a-z]+)', RequestHandler1), # matched the word parameter
    ('/([0-9]+)', RequestHandler2) # matched the numeric parameter
]

因此,您可以根据参数条件分离业务逻辑...

替代方案,为什么不设置get参数?

/application?paramcondition="A" --> URL
/application?paramcondition="0" --> URL

class blabla(webapp2.RequestHandler):
   def get(self):
      param = self.request.get("paramcondition")

      if param == "A":
         # do something
      elif param == "0":
         # do another

所以你只需要1个URL处理程序......