2017-04-22 70 views
0

我已通過webapp2 Route to match all other pathswebapp.WSGIApplication所有其他可能的路徑

走後,我儘量匹配

class SinkHandler(webapp2.RequestHandler): 
    def get(self, *args, **kwargs): 
     self.response.out.write('Sink') 

application = webapp2.WSGIApplication([  
    (r'/<:.*>', SinkHandler) 
], debug = True) 

爲了配合

http://localhost:9080/dummy 
http://localhost:9080/dummy/eummy 
http://localhost:9080/dummy/eummy/fummy 

,但它只是產生404未找到。

我可以這樣做。

class SinkHandler(webapp2.RequestHandler): 
    def get(self, *args, **kwargs): 
     self.response.out.write('Sink') 

application = webapp2.WSGIApplication([  
    (r'/(\w*)$', SinkHandler), 
    (r'/(\w*)/(\w*)$', SinkHandler), 
    (r'/(\w*)/(\w*)/(\w*)$', SinkHandler) 
], debug = True) 

它能夠匹配上述3種類型的URL。但是,如果我需要支持

http://localhost:9080/dummy/eummy/fummy/gummy 

我需要在WSGIApplication

添加額外的條目是有一個更聰明的辦法,以匹配所有其它可能的路徑?

回答

1

這應該工作:

application = webapp2.WSGIApplication([  
    (r'/.*', SinkHandler) 
], debug = True) 

使用更一般的正則表達式,你需要使用Route

from webapp2 import Route 

application = webapp2.WSGIApplication([  
    Route(r'/<:.*>', SinkHandler) 
], debug = True)