我曾经在GAE Python25中执行此操作,使用以下代码处理同一应用中对www.example.com和blog.example.com(注意子域名的差异)的请求的应用内路由:
#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()
但在Python27中,格式有所不同。它是以下内容:
#app.yaml
handlers:
- url: /
script: main.app # (instead of main.py)
#main.py
app = webapp2.WSGIApplication([(r'/', MainPage)],debug=True)
如何在Python27(线程安全)中实现相同的功能,并将不同的子域路由到应用程序中的不同处理程序?
谢谢!
谢谢!
答案 0 :(得分:3)
只需使用webapp2域名路由http://webapp-improved.appspot.com/guide/routing.html#domain-and-subdomain-routing
即可答案 1 :(得分:0)
您需要在处理程序级别执行此操作,如下所示:
#main.py
applications = webapp2.WSGIApplication([('/', GlobalMainHandler)], debug=False)
并在处理程序中:
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()