GAE Google AppEngine - How to handle subdomain in-

2019-08-01 03:02发布

I used to do this in GAE Python25 to handle in-app routing of requests to www.example.com and blog.example.com (Notice the difference in subdomains) within the same app, using the code below:

#app.yaml
- url: /
  script: main.py

#main.py
applications = {
  'www.example.com': webapp.WSGIApplication([('/', MainHandler)],
                                      debug=False),
  'blog.example.com': webapp.WSGIApplication([('/', BlogHandler)],
                                      debug=False)
}

def main():
    host = os.environ['HTTP_HOST']
    if host in applications:
        run_wsgi_app(applications[host])
    else:
        run_wsgi_app(applications['www.example.com'])

if __name__ == '__main__':
  main()

But in Python27, the format is something different. It's the following:

#app.yaml
handlers:
- url: /
  script: main.app  # (instead of main.py)


#main.py
app = webapp2.WSGIApplication([(r'/', MainPage)],debug=True)

How do I achieve the same functionality in Python27 (threadsafe), and route different subdomains to different handlers within the app?

Thanks!

Thanks!

2条回答
Lonely孤独者°
3楼-- · 2019-08-01 03:49

You need todo it on the handler level, something like this:

#main.py
applications = webapp2.WSGIApplication([('/', GlobalMainHandler)], debug=False)

and in the handler:

class GlobalMainHandler(webapp2.RequestHandler):
  def get(self):
    if self.request.host.startswith('blog'): #not sure it is called host, but its there
      self.blog_main()
    else:
      self.the_other_main()
查看更多
登录 后发表回答