当前位置: 首页>>代码示例>>Python>>正文


Python GeoIP.open方法代码示例

本文整理汇总了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"]
开发者ID:urbanbiker,项目名称:UrbanBike,代码行数:40,代码来源:views.py


注:本文中的pygeoip.GeoIP.open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。