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


Python GeoIP.open方法代码示例

本文整理汇总了Python中django.contrib.gis.utils.GeoIP.open方法的典型用法代码示例。如果您正苦于以下问题:Python GeoIP.open方法的具体用法?Python GeoIP.open怎么用?Python GeoIP.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.contrib.gis.utils.GeoIP的用法示例。


在下文中一共展示了GeoIP.open方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test01_init

# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import open [as 别名]
    def test01_init(self):
        "Testing GeoIP initialization."
        g1 = GeoIP() # Everything inferred from GeoIP path
        path = settings.GEOIP_PATH
        g2 = GeoIP(path, 0) # Passing in data path explicitly.
        g3 = GeoIP.open(path, 0) # MaxMind Python API syntax.

        for g in (g1, g2, g3):
            self.assertEqual(True, bool(g._country))
            self.assertEqual(True, bool(g._city))

        # Only passing in the location of one database.
        city = os.path.join(path, 'GeoLiteCity.dat')
        cntry = os.path.join(path, 'GeoIP.dat')
        g4 = GeoIP(city, country='')
        self.assertEqual(None, g4._country)
        g5 = GeoIP(cntry, city='')
        self.assertEqual(None, g5._city)

        # Improper parameters.
        bad_params = (23, 'foo', 15.23)
        for bad in bad_params:
            self.assertRaises(GeoIPException, GeoIP, cache=bad)
            if isinstance(bad, basestring):
                e = GeoIPException
            else:
                e = TypeError
            self.assertRaises(e, GeoIP, bad, 0)
开发者ID:GoSteven,项目名称:Diary,代码行数:30,代码来源:test_geoip.py

示例2: register_location

# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import open [as 别名]
def register_location(request):
     #recuperation ip 
     ip = request.META['REMOTE_ADDR']
     agent = request.META['HTTP_USER_AGENT']
     try:
         gi = GeoIP.open(settings.MEGAVIDEO_CONF['geoip_dat'], GeoIP.GEOIP_STANDARD)

         if ip == '127.0.0.1':
             gir = None
         else:
             gir = gi.record_by_addr(ip)
     except:
         gir = None

     print 'GEOIP ' , gir

     if gir != None:
         latitude = gir['latitude']
         longitude = gir['longitude']
         location = Location.objects.using('megavideo').filter(longitude=longitude).filter(latitude=latitude)

         #se ja existe alterar o counter e pegar a location
         if location.count() > 0:

             location = location[0]
             location.counter = int(location.counter) + 1
             location.save(using='megavideo')

         else: #criar location

             location = Location()
             latitude = gir['latitude']
             longitude = gir['longitude']

             try:
                 cityname = gir['city'].decode('latin1').encode('utf8')
             except:
                 cityname = ''

             countrycode = gir['country_code']
             countryname = gir['country_name'].decode('latin1').encode('utf8')

             location.latitude = latitude
             location.longitude = longitude
             location.city = LocationCity.objects.using('megavideo').get_or_create(name=cityname)[0]
             location.country = LocationCountry.objects.using('megavideo').get_or_create(name=contryname, code=contrycode)[0]
             location.counter = 1
             location.save(using='megavideo')

     else:
         location = None


     #creation de Visitor   
     visit = Visitor()
     visit.ip = VisitorIp.objects.using('megavideo').get_or_create(ip=ip)[0]
     visit.location = location
     visit.agent = VisitorAgent.objects.using('megavideo').get_or_create(agent=agent)[0]
     visit.save(using='megavideo')
     #add geo data
     visit.geo_register()

     request.COOKIES['visitor_id'] = visit.id

     return visit.id
开发者ID:megamidiagroup,项目名称:tcd_offline,代码行数:67,代码来源:views.py


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