質問

Google App Engineでサブドメインを使用するにはどうすればよいですか(Python)。

最初のドメイン部分を取得して、何らかのアクション(ハンドラー)を行います。

例:
      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のアイデアは気に入りましたが、少し異なる問題がありました。 1つの特定のサブドメインを一致させて少し異なるものにしたかったのですが、他のすべてのサブドメインも同じように処理する必要があります。だからここに私の例があります。

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