服务请求时,GAE 自动插入响应标头X-AppEngine-Country
设置为指示发出请求的国家/地区的值。但是,在 GAE 发布响应之前,我希望能够在我的代码片段中使用该值。
我写了这段代码:
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'
有什么方法可以在应用程序中使用这个值吗?
您正在尝试获取响应的标头(您即将做出),而不是请求的标头。试试这个吧。
country = self.request.headers.get('X-AppEngine-Country')
http://code.google.com/appengine/docs/python/tools/webapp/requestclass.html#Request_headers http://code.google.com/appengine/docs/python/tools/webapp/requestclass.html#Request_headers
请求标头,一个类似字典的对象。键不区分大小写。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)