def load_geoip():
import pygeoip
from pygeoip.const import MEMORY_CACHE
resource_package = __name__
filename = pkg_resources.resource_filename(resource_package, 'GeoIP.dat')
return pygeoip.GeoIP(filename, flags=MEMORY_CACHE)
pip install pygeoip
>>> gi = pygeoip.GeoIP('GeoIPRegion.dat')
>>> gi.region_by_name('apple.com')
{'region_code': 'CA', 'country_code': 'US'}
>>> gi = pygeoip.GeoIP('GeoIPISP.dat')
>>> gi.isp_by_name('cnn.com')
'Turner Broadcasting System'
>>> gi = pygeoip.GeoIP('GeoIPASNum.dat')
>>> gi.asn_by_name('cnn.com')
'AS5662 Turner Broadcasting'
def geo(_file, _ip):
''' This function search the geolocation values of an IP address '''
try:
_geo = []
geoDb = pygeoip.GeoIP(_file)
ip_dictionary_values = geoDb.record_by_addr(_ip)
ip_list_values = ip_dictionary_values.items()
for item in ip_list_values:
_geo.append({item[0]:item[1]})
return _geo
except:
pass
def coun(attackerip):
ip_tocoun_db='GeoLiteCity.dat'
gi = pygeoip.GeoIP(ip_tocoun_db)
# IP details are returend as a directory
rec = gi.record_by_name(attackerip)
country = rec['country_name']
country =str(country)
return country
>>> import pygeoip
>>> gi = pygeoip.GeoIP('GeoIP.dat')
>>> gi.country_name_by_addr('64.233.161.99')
'United States'
>>> gi = pygeoip.GeoIP('GeoIP.dat')
>>> gi.country_code_by_name('google.com')
'US'
>>> gi.country_code_by_addr('64.233.161.99')
'US'
>>> gi.country_name_by_addr('64.233.161.99')
'United States'
>>> gi = pygeoip.GeoIP('GeoIPv6.dat')
>>> gi.country_code_by_addr('2a00:1450:400f:802::1006')
'IE'
>>> gi = pygeoip.GeoIP('GeoIPOrg.dat')
>>> gi.org_by_name('dell.com')
'Dell Computer Corporation'
$ pip install flask pygeoip
$ wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
$ gunzip GeoLiteCity.dat.gz