【问题标题】:Is there a way to handle requests with locale-dependant wsgi handlers?有没有办法使用依赖于语言环境的 wsgi 处理程序来处理请求?
【发布时间】:2012-07-11 10:55:54
【问题描述】:

有没有办法使用不同的 WSGI 处理程序来处理来自不同地理位置的请求?具体来说,我想允许来自一个本地(美国)的所有请求,并将所有其他请求重定向到一个保留页面,即类似

application_us = webapp2.WSGIApplication([
    ('/.*', MainHandler)
    ], debug=DEBUG)

application_elsewhere = webapp2.WSGIApplication([
    ('/.*', HoldingPageHandler)
    ], debug=DEBUG)

我知道 X-AppEngine-Country,但我不太确定在这种情况下如何使用它?

【问题讨论】:

    标签: google-app-engine python-2.7 wsgi webapp2


    【解决方案1】:

    您需要做的是只有一个应用程序,如果该国家/地区不受支持,则在处理程序中将用户重定向到 HoldingPageHandler。

    Is it possible to use X-AppEngine-Country within an application。在那里他们解释了如何获得国家

    country = self.request.headers.get('X-AppEngine-Country')
    

    所以你的处理程序会是这样的

    class MainHandler(webapp2.RequestHandler):
      def get(self):
        country = self.request.headers.get('X-AppEngine-Country')
          if country != "US":
            self.redirect_to('hold') #assuming you have a route to hold
          # your logic
    

    【讨论】:

    • 这是一种方法,但 OP 所要求的也是一种完全合法的处理方式。此外,将它放在每个处理程序中是一种严重的次优方法。
    【解决方案2】:

    好的,由 Sebastian Kreft 构建答案我认为将其放入基本处理程序中可能是最简单的,每个其他处理程序都是其子类,如下所示。

    class BaseHandler(webapp2.RequestHandler):
        def __init__(self, *args, **kwargs):
            super(BaseHandler, self).__init__(*args, **kwargs)
            country = self.request.headers.get('X-AppEngine-Country')
            if not country == "US" and not country == "" and not country == None: # The last two handle local development
                self.redirect('international_holding_page')
                logging.info(country)
    

    这更符合 DRY,尽管我不确定这是最有效的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-22
      • 2015-03-15
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多