Is it possible to use X-AppEngine-Country within an application

拟墨画扇 提交于 2019-12-05 02:29:18

问题


When serving a request, GAE automatically inserts response header X-AppEngine-Country set to a value indicating the country from which the request was emitted. However, before GAE issues the response, I’d like to be able to use this value in a snippet of mine.

I wrote this code:

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

But opening the page results in a crash:

  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'

Is there any ways to use this value within the application?


回答1:


You're trying to get the headers of the response (which you're about to make), rather than the headers of the request. Try this instead.

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

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

The request headers, a dictionary-like object. Keys are case-insensitive.



来源:https://stackoverflow.com/questions/9500604/is-it-possible-to-use-x-appengine-country-within-an-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!