加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 是否可以在应用程序中使用X-AppEngine-Country

发布时间:2020-12-20 12:19:41 所属栏目:Python 来源:网络整理
导读:在提供请求时,GAE会自动将响应标头X-AppEngine-Country设置为一个值,该值指示发出请求的国家/地区.但是,在GAE发布响应之前,我希望能够在我的片段中使用此值. 我写了这段代码: class TestPage(webapp2.RequestHandler): def get(self): country = self.respo
在提供请求时,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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读