views:

31

answers:

2

I'm trying to obtain the base URL (hostname) of the server in which my appengine app is running on.

Ie something along the lines of

wsgiref.util.application_uri(self.request.environ)

However it's returning the PATH_INFO which I do not want. Perhaps I'm missing something but even this article states the path info should be omitted. http://docs.python.org/library/wsgiref.html

http://9.latest.my-app.appspot.com

is basically along the lines of what i'm trying to retrieve. Instead it's returning

http://9.latest.my-app.appspot.com/my/requested/path

+1  A: 

You can find the hostname in os.environ['HTTP_HOST'].

That won't include the protocol, but it should be easy to parse from the value you've got:

base = '/'.join(url.split('/')[:3])

or...

import urlparse
url = urlparse.urlparse(url)
base = "%s://%s" % (url.scheme, url.hostname)
Drew Sears
Thanks. I was hoping for a solution that is built in to wsgiref rather than parsing the requested URL. This will suffice however
Kyle
+1  A: 

The request object is a Webob request object. As such, you can get the hostname from self.request.host, the hostname with protocol from self.request.host_url, and so forth.

Nick Johnson