我如何解析uri?

时间:2010-12-21 19:34:31

标签: python google-app-engine httpwebrequest

对于uri http://foo.com/apple/123

我想得到'123'。

根据GAE文档,我可以使用self.request.get()来获取uri,但是有没有帮助只是抓住uri的部分?

2 个答案:

答案 0 :(得分:5)

Python附带的

Yes there is - the urlparse module。你得到的ParseResult将以一种很好的方式剥离主机/协议,然后你就可以使用str的split()来分割路径分隔符。

答案 1 :(得分:4)

如果您正在使用webapp,则可以“捕获”与您的处理程序匹配的正则表达式的部分,并将它们作为参数传递给您的处理程序。例如:

class FooHandler(webapp.RequestHandler):
  def get(self, fruit, number):
    # Do something with your fruit and number (which are both strings, remember!)


application = webapp.WSGIApplication([
    ('/([^/]+)/(\d+)', FooHandler),
])