2

In the question Working with subdomain in google app engine, the following code was suggested.

applications = {
  'product.example.com': webapp.WSGIApplication([
    ('/', IndexHandler),
    ('/(.*)', ProductHandler)]),
  'user.example.com': webapp.WSGIApplication([
    ('/', IndexHandler),
    ('/(.*)', UserHandler)]),
}

def main():
  run_wsgi_app(applications[os.environ['HTTP_HOST']])

if __name__ == '__main__':
  main()

My question is how do I test this locally? When I'm testing it locally, the host is "localhost:8080" and not any of the domains.

Community
  • 1
  • 1
Albert
  • 3,611
  • 3
  • 28
  • 52

1 Answers1

2

Create two new entries in your Hosts file:

127.0.0.1       product.example.com
127.0.0.1       user.example.com

and run your local GAE application on default Http port 80.

If, for some reason, you can't run GAE on port 80, you could try to modify your application.py to match the local port number with something like this:

if os.environ['SERVER_SOFTWARE'].startswith('Dev'):
    PORT=':8080'
else:
    PORT=''

applications = {
  'product.example.com%s' % PORT: webapp.WSGIApplication([
    ('/', IndexHandler),
    ('/(.*)', ProductHandler)]),
  'user.example.com%s' % PORT: webapp.WSGIApplication([
    ('/', IndexHandler),
    ('/(.*)', UserHandler)]),
}

Or even better modifying the main function like this (Thanks to @Nick's comment):

def main():
  run_wsgi_app(applications[os.environ['HTTP_HOST'].split(':')[0]])

You should be ready to test your local application with the following addresses:
http://product.example.com:8080
http://user.example.com:8080

Remember to switch back your Hosts file to be able to reach the Production server.

systempuntoout
  • 71,966
  • 47
  • 171
  • 241
  • ...or you could just go to http://product.example.com:8080/ after setting up the host file entries. – Nick Johnson Mar 28 '11 at 04:19
  • @Nick no, you can't. Hosts file does not allow to specify port information and.. pointing to product.example.com:8080 does not match any WSGIApplication. – systempuntoout Mar 28 '11 at 06:29
  • @systempuntoout I'd forgotten HTTP_HOST includes a port number. A much better approach would be to remove the port number from the header before matching on it, though. – Nick Johnson Mar 28 '11 at 22:38
  • @Nick that would be another viable solution; though I'm not quite sure it is allowed in Production. – systempuntoout Mar 29 '11 at 06:51
  • @systempuntoout Er? Why wouldn't it be? – Nick Johnson Mar 29 '11 at 08:59
  • @Nick How could you strip down a specific header before the request hits the application main? Probably we are talking about two different things. Why don't you post an example :)? – systempuntoout Mar 29 '11 at 09:02
  • @systempuntoout: run_wsgi_app(applications[os.environ['HTTP_HOST'].split(':')[0]]) – Nick Johnson Mar 29 '11 at 11:41
  • aaah Ok, I was thinking about some middleware complex solution. Yes I agree, your method is less invasive. (I have edited my answer accordingly) – systempuntoout Mar 29 '11 at 12:11