1. Install geoip c library first
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. Download The GeoLite City Phthon version.
https://codeload.github.com/maxmind/geoip-api-python/zip/master
unzip master
$ python setup.py build$ python setup.py install
3. Download GeoLiteCity. dat
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
4. Usage
>>> 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'}>>>
IfGirIs NotNone: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 ['longyun'])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 ') >>>
Note:After the installation is complete, run/sbin/ldconfig once. Otherwise, when you import GeoIP, the message "libGeoIP. so.1: cannot open shared object file: No such file or directory "Default libGeoIP. the path of so.1 is/usr/local/lib/libGeoIP. so.1, check/etc/ld. so. whether the conf file contains/usr/local/lib. If not, add the directory/sbin/ldconfig.