[英]Using django GeoIP and MaxMind database
I'm trying to setup geoip in Django to identify the source of a connection (to tailor content for different countries) but running into a problem. 我正在尝试在Django中设置geoip来识别连接的来源(为不同国家定制内容)但遇到问题。
First I execute: 首先我执行:
from django.contrib.gis import geoip
geo = geoip.GeoIP('path to maxmind db')
Then geo.country('www.google.com')
returns the US as you'd expect. 然后geo.country('www.google.com')
按照您的预期返回美国。 Other popular websites also work fine. 其他热门网站也可以正常使用。
However when I try it on my own client IP I get an empty record. 但是,当我在自己的客户端IP上尝试它时,我得到一个空记录。 For example: geo.country('127.6.89.129')
例如: geo.country('127.6.89.129')
returns {'country_name': None, 'country': None}
返回{'country_name': None, 'country': None}
What am I missing here? 我在这里错过了什么? Does the maxmind database only cover popular sites so can't be used if I want to identify the source of the connection? maxmind数据库是否仅覆盖热门站点,因此如果我想识别连接源,则无法使用它?
I'm also using the browser locale settings to identify language but unfortunately I need geo-location to tailor some of the content independently of language. 我也使用浏览器区域设置来识别语言,但不幸的是我需要地理位置来定制一些独立于语言的内容。
您在示例中使用的IP地址是本地IP地址,您不能在网络外使用它,您是否尝试使用真实的公共IP地址?
Your ip could be forwarded 您的IP可以转发
def foo(request):
g = GeoIP()
country = g.country(get_client_ip(request))
country_code = country['country_code']
def get_client_ip(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')
return ip
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.