ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • <PYTHON>[pygeoip]
    Flower in my dev/Python 2015. 9. 9. 16:23

    [pygeoip]


    - 설치 : pip install pygeoip


    - 참조

    https://code.google.com/p/pygeoip/

    https://pypi.python.org/pypi/pygeoip/


    - 지도 db


    GeoIP.dat


    GeoLiteCity.dat : http://dev.maxmind.com/geoip/legacy/geolite/


    -활용 코드

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import pygeoip
     
    fName = ['GeoIP.dat','GeoLiteCity.dat']
     
    gi = pygeoip.GeoIP(fName[0])
    print "[Google Info...(GeoIP.dat)]"
    print gi.country_code_by_name('google.com')
    print gi.country_code_by_addr('64.233.161.99')
    print gi.country_name_by_name('google.com')
    print gi.country_name_by_addr('64.233.161.99')
     
    print "[Google Info...(GeoLiteCity.dat)]"
    gic = pygeoip.GeoIP(fName[1])
    print gic.record_by_addr('64.233.161.99')
    print gic.record_by_name('google.com')
    print gic.region_by_name('google.com')
    print gic.region_by_addr('64.233.161.99')
    print gic.time_zone_by_name('google.com')
    print gic.time_zone_by_addr('64.233.161.99')
    cs


    -코드 결과


    -ipython 테스트 결과

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    In [2]: import pygeoip
     
    In [3]: fName = ['GeoIP.dat','GeoLiteCity.dat']
     
    In [4]: gi = pygeoip.GeoIP(fName[0])
     
    In [5]: gi.country_code_by_name('google.com')
    Out[5]: 'US'
     
    In [6]: gi.country_code_by_addr('64.233.161.99')
    Out[6]: 'US'
     
    In [7]: gi.country_name_by_name('google.com')
    Out[7]: 'United States'
     
    In [8]: gi.country_name_by_addr('64.233.161.99')
    Out[8]: 'United States'
     
    In [9]: gic = pygeoip.GeoIP(fName[1])
     
    In [10]: gic.record_by_addr('64.233.161.99')
    Out[10]:
    {'area_code'650,
     'city': u'Mountain View',
     'continent''NA',
     'country_code''US',
     'country_code3''USA',
     'country_name''United States',
     'dma_code'807,
     'latitude'37.41919999999999,
     'longitude'-122.0574,
     'metro_code''San Francisco, CA',
     'postal_code': u'94043',
     'region_code': u'CA',
     'time_zone''America/Los_Angeles'}
     
    In [11]: gic.record_by_name('google.com')
    Out[11]:
    {'area_code'650,
     'city': u'Mountain View',
     'continent''NA',
     'country_code''US',
     'country_code3''USA',
     'country_name''United States',
     'dma_code'807,
     'latitude'37.41919999999999,
     'longitude'-122.0574,
     'metro_code''San Francisco, CA',
     'postal_code': u'94043',
     'region_code': u'CA',
     'time_zone''America/Los_Angeles'}
     
    In [12]: gic.region_by_name('google.com')
    Out[12]: {'country_code''US''region_code': u'CA'}
     
    In [13]: gic.region_by_addr('64.233.161.99')
    Out[13]: {'country_code''US''region_code': u'CA'}
     
    In [14]: gic.time_zone_by_name('google.com')
    Out[14]: 'America/Los_Angeles'
     
    In [15]: gic.time_zone_by_addr('64.233.161.99')
    Out[15]: 'America/Los_Angeles'
    cs


    'Flower in my dev > Python' 카테고리의 다른 글

    <PYTHON>[LOG]  (0) 2015.09.11
    <PYTHON>[pygoogle]  (0) 2015.09.09
    <PYTHON>[cPickle][msgpack][marshal]  (0) 2015.09.09
    <PYTHON>[paramiko sftp]  (0) 2015.09.03
    <PYTHON>[json]  (0) 2015.08.21

    댓글

Designed by Tistory.