views:

644

answers:

2

Hi all,

I'm trying to get the server URL of a currently running appengine java app from code. That is, if the app is running on my local dev machine I would like to somehow get returned "http://localhost:8080" but if it is running in prod I'd like to be returned "http://myappid.appspot.com". Are there any java or appengine API's that can do this? I'd like to not have a to manually change and read from a config file or a constant.

Thanks.

  • Aleem
+3  A: 

Here's a couple of way to do it in your request handler (if you're using the provided webapp elementary framework):

  def get(self):
    self.response.out.write(self.request.headers.get('host', 'no host'))
    self.response.out.write('<br>\n')
    who = wsgiref.util.request_uri(self.request.environ)
    self.response.out.write(who + '<br>\n')

This emits 'localhost:8081' or 'blabla.appspot.com' as the first line, and as the second like the complete URI instead, e.g. 'http://localhost:8081/zup' or 'http://blabla.appspot.com/zup'.

More generally, you can use wsgiref.util to easily extract info out of any WSGI environment, and since App Engine serves on top of WSGI there should always be easy ways to pry such an environment from the clutches of whatever framework you've chosen;-)

Alex Martelli
Java, not Python!
Nick Johnson
Thanks for giving the Python answer, though, as someone who came along looking for the answer in Python.
Gordon Worley
@Gordon, glad this proved useful to you (even though I hadn't taken proper notice of the tags!).
Alex Martelli
+1  A: 

You should be able to use getServerName():

boolean local = "localhost".equals(httpServletRequest.getServerName());

There are other methods you can also use if you need more info, e.g. getServerPort().

pjesi
Great that worked - thanks!
aloo