本文整理汇总了Python中pygeoip.GeoIP.open方法的典型用法代码示例。如果您正苦于以下问题:Python GeoIP.open方法的具体用法?Python GeoIP.open怎么用?Python GeoIP.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygeoip.GeoIP
的用法示例。
在下文中一共展示了GeoIP.open方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_location
# 需要导入模块: from pygeoip import GeoIP [as 别名]
# 或者: from pygeoip.GeoIP import open [as 别名]
def get_location(request, default=MINSK):
location = request.session.get("location", None)
if location and len(str(location[0])) < 4:
return location
remote_addr = request.META["REMOTE_ADDR"]
if remote_addr == "127.0.0.1":
request.session["location"] = default
request.session["django_timezone"] = pytz.timezone("Europe/Minsk")
return default
gi = GeoIP.open(settings.GEOIP_CITY, GeoIP.GEOIP_INDEX_CACHE | GeoIP.GEOIP_CHECK_CACHE)
geoip = gi.record_by_name(remote_addr)
if not geoip:
request.session["location"] = default
return default
time_zone = geoip.get("time_zone", None)
if time_zone:
request.session["django_timezone"] = pytz.timezone(time_zone)
country = geoip.get("country_name", None)
if country:
request.session["country"] = country
country_code = geoip.get("country_code", None)
if country_code:
request.session["country_code"] = country_code
city = geoip.get("city", None)
if city:
request.session["city"] = city
lat = geoip.get("latitude", None)
lng = geoip.get("longitude", None)
request.session["location"] = [lat, lng]
else:
request.session["city"] = "Minsk"
request.session["location"] = default
# wtf ?
return request.session["location"]