将URL模式映射到WSGIApplication中的单个RequestHandler

Mar*_*ddy 3 python google-app-engine

是否可以将URL模式(正则表达式或其他映射)映射到单个RequestHandler?如果是这样我怎么能做到这一点?

理想情况下,我想做这样的事情:

application=WSGIApplication([('/*',MyRequestHandler),])
Run Code Online (Sandbox Code Playgroud)

这样MyRequestHandler就可以处理所有请求.请注意,我正在开发一个概念证明应用程序,根据定义,我不会知道将要访问域的所有URL.另请注意,如果重要的话,我会在Google App Engine上执行此操作.

Nic*_*son 8

您描述的模式将正常工作.此外,您指定的正则表达式中的任何组都将作为参数传递给处理程序方法(get,post等).例如:

class MyRequestHandler(webapp.RequestHandler):
  def get(self, date, id):
    # Do stuff. Note that date and id are both strings, even if the groups are numeric.

application = WSGIApplication([('/(\d{4}-\d{2}-\d{2})/(\d+)', MyRequestHandler)])
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,两个组(日期和id)被分解并作为参数传递给处理函数.