是否可以在应用程序中使用X-AppEngine-Country [英] Is it possible to use X-AppEngine-Country within an application

查看:105
本文介绍了是否可以在应用程序中使用X-AppEngine-Country的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在提供请求时,GAE会自动将响应标头 X-AppEngine-Country 设置为指示发出请求的国家/地区的值。但是,在GAE发布响应之前,我希望能够在我的一个片段中使用此值。

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.

我编写了这段代码:
$ b

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)

页面导致崩溃:

  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?

推荐答案

'试图获得响应的头部(你将要做的),而不是请求的头部。

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.

这篇关于是否可以在应用程序中使用X-AppEngine-Country的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆