문제

Google App Engine (Python)에서 Sub Domain에서 어떻게 작업 할 수 있습니까?

첫 번째 도메인 부분을 얻고 조치를 취하고 싶습니다 (핸들러).

예시:
Product.example.com-> 제품 핸들러로 보냅니다
user.example.com-> 사용자 핸들러로 보냅니다

실제로 가상 경로를 사용 하여이 코드가 있습니다.

  application = webapp.WSGIApplication(
    [('/', IndexHandler),
     ('/product/(.*)', ProductHandler),
     ('/user/(.*)', UserHandler)
  ]
도움이 되었습니까?

해결책

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 서브 클래스를 작성할 수 있습니다.

다른 팁

나는 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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top