mirror of
https://github.com/KevinMidboe/Node-Com-Handler.git
synced 2025-10-29 09:40:27 +00:00
Created files that make classes containing info from geoLite ip database
based on given IP address
This commit is contained in:
39
ipLookup.py
Normal file
39
ipLookup.py
Normal 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
18
testIpLookup.py
Executable 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)
|
||||
Reference in New Issue
Block a user