From 94041dfc5b757174f6e73b4dc56de533c834fd1c Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Thu, 8 Dec 2016 11:46:54 +0100 Subject: [PATCH] Created files that make classes containing info from geoLite ip database based on given IP address --- ipLookup.py | 39 +++++++++++++++++++++++++++++++++++++++ testIpLookup.py | 18 ++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 ipLookup.py create mode 100755 testIpLookup.py diff --git a/ipLookup.py b/ipLookup.py new file mode 100644 index 0000000..3002ae7 --- /dev/null +++ b/ipLookup.py @@ -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__) \ No newline at end of file diff --git a/testIpLookup.py b/testIpLookup.py new file mode 100755 index 0000000..d152f97 --- /dev/null +++ b/testIpLookup.py @@ -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)