Skip to content

Instantly share code, notes, and snippets.

@hanksudo
Created May 4, 2015 16:27
Show Gist options
  • Save hanksudo/d42c3ca92b4d4ecd3377 to your computer and use it in GitHub Desktop.
Save hanksudo/d42c3ca92b4d4ecd3377 to your computer and use it in GitHub Desktop.
(Python ) geoip2 test script
# -*- coding: utf-8 -*-
from os import path
import geoip2.database
ips = """
223.143.56.227
101.8.221.38
1.34.73.244
42.70.123.135
14.0.209.138
14.0.209.55
223.143.128.158
223.136.202.186
118.167.211.101
36.225.233.245
42.74.60.35
121.202.126.34
42.70.50.94
121.202.253.144
220.133.100.235
14.0.143.104
111.249.242.126
42.75.48.195
223.136.43.0
110.29.149.220
101.13.147.13
111.80.250.86
101.9.240.107
"""
def getIPinfo(ip):
response = reader.city(ip)
print ip
print response.country.iso_code
# 'US'
print response.country.name
# 'United States'
print response.country.names['zh-CN'].encode('utf-8')
print response.subdivisions.most_specific.name
# 'Minnesota'
print response.subdivisions.most_specific.iso_code
# 'MN'
print response.city.name
# 'Minneapolis'
print response.postal.code
# '55455'
print response.location.latitude
# 44.9733
print response.location.longitude
# -93.2323
print "========================"
if __name__ == '__main__':
mmdb_path = path.join(path.dirname(path.abspath(__file__)), 'GeoLite2-City.mmdb')
reader = geoip2.database.Reader(mmdb_path)
for ip in ips.split('\n'):
try:
getIPinfo(ip)
except Exception as e:
print e.message
reader.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment