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

时间:2023-01-24 17:49:30

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

如何在谷歌应用引擎(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

示例:product.example.com - >将其发送到产品处理程序user.example.com - >将其发送给用户处理程序

Actually, using virtual path I have this code:

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

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

2 个解决方案

#1


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

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

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

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

#2


I liked the idea from Nick but I had a slightly different issue. I wanted to match one specific subdomain to handle it a bit different, but all other sub domains should be handled the same. So here is my example.

我喜欢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()

#1


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

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

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

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

#2


I liked the idea from Nick but I had a slightly different issue. I wanted to match one specific subdomain to handle it a bit different, but all other sub domains should be handled the same. So here is my example.

我喜欢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()