views:

1441

answers:

1

How can I work with sub domain in google app engine (python).

I wanna get first domain part and take some action (handler).

Example:
     product.example.com -> send it to products handler
     user.example.com -> send it to users handler

Actually, using virtual path I have this code:

  application = webapp.WSGIApplication(
    [('/', IndexHandler),
     ('/product/(.*)', ProductHandler),
     ('/user/(.*)', UserHandler)
  ]

Did I make my self clear? (sorry poor english)

+7  A: 

WSGIApplication isn't capable of routing based on domain. Instead, you need to create a separate application for each subdomain, like this:

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()

Alternately, you could write your own WSGIApplication subclass that knows how to handle multiple hosts.

Nick Johnson
Thank you! Do you have some sample of this sub WSGIApplication to me? I'm stating with python...
Zote
Check out the source for the current one at http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/ext/webapp/__init__.py - modifying the __call__ method to take into account the hostname should be fairly straightforward.
Nick Johnson