15

How do I get App Engine to generate the URL of the server it is currently running on?

If the application is running on development server it should return

http://localhost:8080/

and if the application is running on Google's servers it should return

http://application-name.appspot.com
0

3 Answers 3

16

You can get the URL that was used to make the current request from within your webapp handler via self.request.url or you could piece it together using the self.request.environ dict (which you can read about on the WebOb docs - request inherits from webob)

You can't "get the url for the server" itself, as many urls could be used to point to the same instance.

If your aim is really to just discover wether you are in development or production then use:

'Development' in os.environ['SERVER_SOFTWARE']
Sign up to request clarification or add additional context in comments.

2 Comments

+1 For trying to guess at actual intent and letting him know about the dev server flag.
self.request.host is a better way to get just the current hostname, or self.request.host_url if you want the protocol prefix.
14

Here is an alternative answer.

from google.appengine.api import app_identity
server_url = app_identity.get_default_version_hostname()

On the dev appserver this would show:

localhost:8080

and on appengine

your_app_id.appspot.com

1 Comment

Google seems to have depreciated this for python 3 cloud.google.com/appengine/docs/standard/python/refdocs/…
0

If you're using webapp2 as framework chances are that you already using URI routing in you web application.
http://webapp2.readthedocs.io/en/latest/guide/routing.html

app = webapp2.WSGIApplication([
    webapp2.Route('/', handler=HomeHandler, name='home'),
])

When building URIs with webapp2.uri_for() just pass _full=True attribute to generate absolute URI including current domain, port and protocol according to current runtime environment.

uri = uri_for('home')
# /

uri = uri_for('home', _full=True)
# http://localhost:8080/
# http://application-name.appspot.com/
# https://application-name.appspot.com/
# http://your-custom-domain.com/

This function can be used in your Python code or directly from templating engine (if you register it) - very handy.

Check webapp2.Router.build() in the API reference for a complete explanation of the parameters used to build URIs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.