在谷歌应用引擎中使用子域名

时间:2009-05-08 03:15:09

标签: python google-app-engine subdomain

如何在google app engine(python)中使用子域。

我想获得第一个域名部分并采取一些行动(处理程序)。

实施例
product.example.com - >将其发送给产品经销商
user.example.com - >将其发送给用户处理程序

实际上,使用虚拟路径我有这个代码:

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

2 个答案:

答案 0 :(得分:26)

WSGIApplication无法基于域进行路由。相反,您需要为每个子域创建一个单独的应用程序,如下所示:

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

或者,您可以编写自己的WSGIApplication子类,该子类知道如何处理多个主机。

答案 1 :(得分:2)

我喜欢Nick的想法,但我的问题略有不同。我想匹配一个特定的子域来处理它有点不同,但所有其他子域应该处理相同。所以这是我的榜样。

import os

def main():
   if (os.environ['HTTP_HOST'] == "sub.example.com"):
      application = webapp.WSGIApplication([('/(.*)', OtherMainHandler)], debug=True)
   else:
      application = webapp.WSGIApplication([('/', MainHandler),], debug=True)

   run_wsgi_app(application)


if __name__ == '__main__':
   main()
相关问题