Python安裝MaxMindGeoLiteCity

來源:互聯網
上載者:User

1、先安裝 geoip c library
 geoip c library >= 1.4.6 installed on your machine. >= 1.4.6 installed on your machine.
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.7.tar.gztar -xvzf GeoIP-1.4.7.tar.gzcd GeoIP-1.4.7./confiuremake make install 
2、下載GeoLite City Phthon版本
https://codeload.github.com/maxmind/geoip-api-python/zip/master
unzip master
$ python setup.py build$ python setup.py install

3、下載GeoLiteCity.dat
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz 
gunzip GeoLiteCity.dat.gz 
4、用法
>>> import GeoIP
>>> geodb='/home/qinjianwang/maxMind/GeoLiteCity.dat'
>>> gi = GeoIP.open(geodb, GeoIP.GEOIP_STANDARD)                                      
>>> ip='180.76.1.3'
>>> gi.record_by_addr(ip)
{'city': 'Beijing', 'region_name': 'Beijing', 'region': '22', 'area_code': 0, 'time_zone': 'Asia/Harbin', 'longitude': 116.38829803466797, 'metro_code': 0, 'country_code3': 'CHN', 'latitude': 39.92890167236328, 'postal_code': None, 'dma_code': 0, 'country_code': 'CN', 'country_name': 'China'}
>>> gir = gi.record_by_name("www.google.com")
>>> gir
{'city': 'Mountain View', 'region_name': 'California', 'region': 'CA', 'area_code': 650, 'time_zone': 'America/Los_Angeles', 'longitude': -122.05740356445312, 'metro_code': 807, 'country_code3': 'USA', 'latitude': 37.4192008972168, 'postal_code': '94043', 'dma_code': 807, 'country_code': 'US', 'country_name': 'United States'}
>>> 
if gir is not None:    print(gir['country_code'])    print(gir['country_code3'])    print(gir['country_name'])    print(gir['city'])    print(gir['region'])    print(gir['region_name'])    print(gir['postal_code'])    print(gir['latitude'])    print(gir['longitude'])    print(gir['area_code'])    print(gir['time_zone'])    print(gir['metro_code'])    print(str(gir))>>> gir['country_code'],gir['city'],gir['region_name']    ('US', 'Mountain View', 'California')>>> 
注意:安裝完成後,最好執行一次 /sbin/ldconfig,否則在import GeoIP時,可能會報:“ libGeoIP.so.1: cannot open shared object file: No such file or directory"預設libGeoIP.so.1的路徑是 /usr/local/lib/libGeoIP.so.1,需要檢查/etc/ld.so.conf檔案中是否有包含:/usr/local/lib若沒有,則加上此目錄再/sbin/ldconfig。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.