使用WSGIRequest时,X-AppEngine-CityLatLong未在Google App Engine Django 1.5.11上填充

时间:2021-09-19 23:17:03

I'm not getting the X-AppEngine-CityLatLong (or City or Country) values back in the POST header when I submit a POST to my WSGI Django 1.5.11 Python app on Google App Engine.

当我向Google App Engine上的WSGI Django 1.5.11 Python应用程序提交POST时,我没有在POST标题中返回X-AppEngine-CityLatLong(或City或Country)值。

My wsgi.py is typical:

我的wsgi.py是典型的:

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.settings")
application = get_wsgi_application()  

from which I get a request object via my urls.py the normal way. My request attributes such as request.method, etc. are all populated correctly. However, when I look for the GAE-injected headers to lookup the geolocation of the client:

我通过我的urls.py以正常方式从中获取请求对象。我的请求属性(如request.method等)都已正确填充。但是,当我查找GAE注入的标头来查找客户端的地理位置时:

@staticmethod
def get_demographics(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    if request.method == 'GET':
        slatlong = request.GET.get("X-AppEngine-CityLatLong")
        lat = slatlong.split(",")[0]
        lng = slatlong.split(",")[1]
        city = request.GET.get("X-AppEngine-City")
        country = request.GET.get("X-AppEngine-Country")
    elif request.method == 'POST':
        slatlong = request.POST.get("X-AppEngine-CityLatLong")
        lat = slatlong.split(",")[0]
        lng = slatlong.split(",")[1]
        city = request.POST.get("X-AppEngine-City")
        country = request.POST.get("X-AppEngine-Country")
    else:
        lat = 0
        lng = 0
        city = 'unknown'
        country = 'unknown'
    return ip, lat, lng, city, country

each of the request.POST.gets, for example:

每个request.POST.gets,例如:

request.POST.get("X-AppEngine-CityLatLong")

returns a NoneType, instead of (in the case of CityLatLong) the expected comma-delimited string (e.g. "1.234,-5.678"). This causes the subsequent string split meant to separate the lat/long to throw a HTTP 500 with:

返回NoneType,而不是(在CityLatLong的情况下)预期的逗号分隔字符串(例如“1.234,-5.678”)。这导致后续的字符串拆分意味着将lat / long分开以抛出HTTP 500:

"NoneType" object has no attribute "split"

“NoneType”对象没有属性“split”

Some code examples on * say to use request.getHeader for this, but when I do that I get a HTTP 500 with:

*上的一些代码示例说使用request.getHeader,但是当我这样做时,我得到一个HTTP 500:

"WSGIRequest" object has no attribute "getHeader"

“WSGIRequest”对象没有属性“getHeader”

Any solution much appreciated.

任何解决方案非常赞赏

More info on the X-AppEngine headers: https://cloud.google.com/appengine/docs/python/requests#Python_Request_headers

有关X-AppEngine标头的更多信息:https://cloud.google.com/appengine/docs/python/requests#Python_Request_headers

1 个解决方案

#1


0  

UPDATE: FIXED

The actual calls to use are:

实际使用的电话是:

request.META.get('HTTP_X_APPENGINE_CITYLATLONG')
request.META.get('HTTP_X_APPENGINE_CITY')
request.META.get('HTTP_X_APPENGINE_COUNTRY')
request.META.get('HTTP_X_APPENGINE_REGION')

#1


0  

UPDATE: FIXED

The actual calls to use are:

实际使用的电话是:

request.META.get('HTTP_X_APPENGINE_CITYLATLONG')
request.META.get('HTTP_X_APPENGINE_CITY')
request.META.get('HTTP_X_APPENGINE_COUNTRY')
request.META.get('HTTP_X_APPENGINE_REGION')