【问题标题】:Is it possible to use X-AppEngine-Country within an application是否可以在应用程序中使用 X-AppEngine-Country
【发布时间】:2012-02-29 14:08:56
【问题描述】:

在处理请求时,GAE 会自动插入响应标头 X-AppEngine-Country,并设置为指示发出请求的国家/地区的值。但是,在 GAE 发出响应之前,我希望能够在我的 sn-p 中使用这个值。

我写了这段代码:

class TestPage(webapp2.RequestHandler):
    def get(self):
        country = self.response.headers["X-AppEngine-Country"]
        self.response.out.write("<pre>country %s </pre>" % country)

但是打开页面会导致崩溃:

  File "/base/python27_runtime/python27_lib/versions/third_party/webob-1.1.1/webob/headers.py", line 16, in __getitem__
    raise KeyError(key)
KeyError: 'x-appengine-country'

有没有办法在应用程序中使用这个值?

【问题讨论】:

  • 为什么你认为响应应该有国家代码?为什么您需要在响应中包含国家/地区代码? \
  • @ShayErlichmen 我认为你没有抓住重点。响应具有国家/地区代码,因为 Google App Engine 插入了它。 more here
  • repose 是输出请求是输入,我认为你混淆了响应和请求

标签: python google-app-engine geolocation


【解决方案1】:

您正在尝试获取响应的标头(您将要做的),而不是请求的标头。试试这个吧。

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

http://code.google.com/appengine/docs/python/tools/webapp/requestclass.html#Request_headers

请求标头,一个类似字典的对象。键不区分大小写。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多