Created files that make classes containing info from geoLite ip database

based on given IP address
This commit is contained in:
2016-12-08 11:46:54 +01:00
parent 27815e80a7
commit 94041dfc5b
2 changed files with 57 additions and 0 deletions

39
ipLookup.py Normal file
View File

@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2016-11-22 12:07:08
# @Last Modified by: KevinMidboe
# @Last Modified time: 2016-11-28 08:14:35
import geoip2.database
import ipaddress, os
class ipLookup(object):
"""docstring for ipLookup"""
def __init__(self, address):
super(ipLookup, self).__init__()
self.databaseLocation = 'GeoLiteDatabases/city.mmdb'
self.address = ipaddress.ip_address(address)
self.version = self.address.version
self.getLocation()
def getLocation(self):
reader = geoip2.database.Reader(self.databaseLocation)
response = reader.city(self.address)
self.country = response.country.name
self.city = response.city.name
self.region = response.subdivisions.most_specific.name
self.postcode = response.postal.code
def changeDatabase(self, database):
newDatabaseLocation = 'GeoLiteDatabases/'+database+'.mmdb'
if os.path.isfile(newDatabaseLocation):
self.databaseLocation = newDatabaseLocation
else:
raise NameError('File '+newDatabaseLocation+' does not exist')
def getDatabaseLocation(self):
return self.databaseLocation
def __str__(self):
return str(self.__class__) + ": " + str(self.__dict__)

18
testIpLookup.py Executable file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: KevinMidboe
# @Date: 2016-11-22 12:18:17
# @Last Modified by: KevinMidboe
# @Last Modified time: 2016-12-01 20:12:48
import ipLookup
if __name__ == '__main__':
ipList = {}
ip = '91.22.128.66'
ip = '85.164.178.87'
if ip not in ipList:
ipList.update({ip:ipLookup.ipLookup(ip)})
address = ipList[ip]
print(address)