Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the base URI in AppEngine?

How can I get the base URI in a Google AppEngine app written in Python? I'm using the webapp framework.

e.g.

http://example.appspot.com/
like image 718
Gelatin Avatar asked Sep 12 '10 23:09

Gelatin


1 Answers

The proper way to parse self.request.url is not with a regular expression, but with Python standard library's urlparse module:

import urlparse

...

o = urlparse.urlparse(self.request.url)

Object o will be an instance of the ParseResult class with string-valued fields such as o.scheme (probably http;-) and o.netloc ('example.appspot.com' in your case). You can put some of the strings back together again with the urlparse.urlunparse function from the same module, e.g.

s = urlparse.urlunparse((o.scheme, o.netloc, '', '', '', ''))

which would give you in s the string 'http://example.appspot.com' in this case.

like image 195
Alex Martelli Avatar answered Oct 20 '22 01:10

Alex Martelli