For the uri http://foo.com/apple/123
I want to get '123'.
According to the GAE documentation I can get the uri using self.request.ge开发者_运维技巧t(), however is there a helper for just grabbing the sections of the uri?
Yes there is - the urlparse
module that comes with Python. The ParseResult
that you get back will strip off the host/protocol and such in a nice way, and then you can just use str's split()
to split on the path separator.
If you're using webapp, you can 'capture' parts of the regular expression that matches your handler, and they'll be passed to your handler as arguments. For example:
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),
])
精彩评论