def fetch_geoip(ip_address, language=None): # Prepare response object response = {} # Get hostname from IP address, pass if fail try: response['ip_address'] = ip_address response['hostname'] = socket.gethostbyaddr(ip_address)[0] except Exception as ex: pass # Load GeoLite2 City database geoip = Reader(path.join(config.MMDB_PATH, "GeoLite2-City.mmdb")) # Try to fetch data and build response, otherwise raise exception try: data = geoip.city(ip_address) # geoip.city response['city'] = { "name": data.city.name, "id": data.city.geoname_id, "region": data.subdivisions.most_specific.name, "region_code": data.subdivisions.most_specific.iso_code } # geoip.country response['country'] = { "name": data.country.name, "iso_code": data.country.iso_code, "continent": data.continent.name, "continent_code": data.continent.code, "is_eu": data.country.is_in_european_union } # geoip.location response['location'] = { "accuracy_radius": data.location.accuracy_radius, "zip": data.postal.code, "latitude": data.location.latitude, "longitude": data.location.longitude, "timezone": data.location.time_zone } except AddressNotFoundError as ex: raise ex # Close database instances geoip.close() # Load GeoLite2 ASN database (optional) response['asn'] = fetch_asn(ip_address) # Return built response object return response
def getisp(ip): loc_info = {} r = Reader(join(abspath(dirname(__file__)), 'GeoIP2-ISP.mmdb')) obj = r.isp(ip) print obj.raw loc_info['isp'] = { 'organization': obj.raw.get('autonomous_system_organization', ''), 'isp': obj.raw.get('isp', ''), } print loc_info r.close()
def fetch_asn(ip_address): # Check if INCLUDE_ASN is True before proceeding if not config.INCLUDE_ASN: return None # Load GeoLite2 ASN database geoasn = Reader(path.join(config.MMDB_PATH, "GeoLite2-ASN.mmdb")) # Try to fetch data and build response, otherwise return empty try: data = geoasn.asn(ip_address) return { "name": data.autonomous_system_organization, "id": data.autonomous_system_number } except AddressNotFoundError: return {} finally: geoasn.close()
def get_lng_lat(ip_address): """ Takes an IPv4 or IPv6 address and returns a 2-tuple of (longitude, latitude). """ coord = None if ip_address and is_public_ip_addr(ip_address): city_db_path = get_city_db_path() db_reader = Reader(city_db_path) try: result = db_reader.city(ip_address) coord = (result.location.longitude, result.location.latitude) except AddressNotFoundError: _LOGGER.warning('The address %s is not in the GeoLite2 database.', ip_address) finally: db_reader.close() return coord
class MaxMindEnterpriseDatabase(BaseDriver): """ MaxMind Enterprise Database """ def __init__(self, database): """ Constructor :param database: Location of the enterprise-database """ self._reader = Reader(database) # Close reader when app closes down atexit.register(lambda: self._reader.close()) def insights(self, ip): """ Get insights in ip :param ip: The ip :return: Insights :rtype: geoip2.models.City """ return self._reader.enterprise(ip)
class GeoIPHandler(object): def __init__(self, data_folder, maxmind_license_key): self.data_folder = data_folder self.maxmind_license_key = maxmind_license_key self.dbfile = abspath(join(self.data_folder, 'GeoLite2-City.mmdb')) self.logger = getLogger() self.reader = None self.reader_manager(action='open') self.logger.info('Opening persistent connection to the MaxMind DB...') def reader_manager(self, action=None): if action == 'open': try: self.reader = Reader(self.dbfile) except FileNotFoundError: self.logger.error("Could not find MaxMind DB! Downloading!") result_status = self.download() if result_status: self.logger.error( "Could not download MaxMind DB! You may need to manually install it." ) exit(1) else: self.reader = Reader(self.dbfile) else: self.reader.close() def lookup(self, ipaddress): ip = ipaddress self.logger.debug( 'Getting lat/long for Tautulli stream using ip with last octet ending in %s', ip.split('.')[-1:][0]) return self.reader.city(ip) def update(self): today = date.today() try: dbdate = date.fromtimestamp(stat(self.dbfile).st_mtime) db_next_update = date.fromtimestamp(stat( self.dbfile).st_mtime) + timedelta(days=30) except FileNotFoundError: self.logger.error("Could not find MaxMind DB as: %s", self.dbfile) self.download() dbdate = date.fromtimestamp(stat(self.dbfile).st_mtime) db_next_update = date.fromtimestamp(stat( self.dbfile).st_mtime) + timedelta(days=30) if db_next_update < today: self.logger.info("Newer MaxMind DB available, Updating...") self.logger.debug( "MaxMind DB date %s, DB updates after: %s, Today: %s", dbdate, db_next_update, today) self.reader_manager(action='close') self.download() self.reader_manager(action='open') else: db_days_update = db_next_update - today self.logger.debug("MaxMind DB will update in %s days", abs(db_days_update.days)) self.logger.debug( "MaxMind DB date %s, DB updates after: %s, Today: %s", dbdate, db_next_update, today) def download(self): tar_dbfile = abspath(join(self.data_folder, 'GeoLite2-City.tar.gz')) maxmind_url = ( 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City' f'&suffix=tar.gz&license_key={self.maxmind_license_key}') downloaded = False retry_counter = 0 while not downloaded: self.logger.info('Downloading GeoLite2 DB from MaxMind...') try: urlretrieve(maxmind_url, tar_dbfile) downloaded = True except URLError as e: self.logger.error("Problem downloading new MaxMind DB: %s", e) result_status = 1 return result_status except HTTPError as e: if e.code == 401: self.logger.error( "Your MaxMind license key is incorect! Check your config: %s", e) result_status = 1 return result_status else: self.logger.error( "Problem downloading new MaxMind DB... Trying again: %s", e) sleep(2) retry_counter = (retry_counter + 1) if retry_counter >= 3: self.logger.error( "Retried downloading the new MaxMind DB 3 times and failed... Aborting!" ) result_status = 1 return result_status try: remove(self.dbfile) except FileNotFoundError: self.logger.warning( "Cannot remove MaxMind DB as it does not exist!") self.logger.debug("Opening MaxMind tar file : %s", tar_dbfile) tar = taropen(tar_dbfile, 'r:gz') for files in tar.getmembers(): if 'GeoLite2-City.mmdb' in files.name: self.logger.debug('"GeoLite2-City.mmdb" FOUND in tar file') files.name = basename(files.name) tar.extract(files, self.data_folder) self.logger.debug('%s has been extracted to %s', files, self.data_folder) tar.close() try: remove(tar_dbfile) self.logger.debug('Removed the MaxMind DB tar file.') except FileNotFoundError: self.logger.warning( "Cannot remove MaxMind DB TAR file as it does not exist!")
geoip2_reader = Reader('GeoLite2-City.mmdb') for _ip, _cnt in stat_all['vistors'].items(): _city_name = 'unknow' try: _city = geoip2_reader.city(_ip) _city_name = '{}/{}'.format(_city.country.names.get('en', ''), _city.city.names.get('en', '')) #_city_name = '{}/{}'.format(_city.country.names.get('zh-CN', ''), _city.city.names.get('zh-CN', '')) except BaseException as e: print(e) pass stat_all['city'].setdefault(_city_name, 0) stat_all['city'][_city_name] += _cnt geoip2_reader.close() # 打印结果 print('=' * 70) print('|1. 概览{:>61}|'.format('')) print('-' * 70) print('| 总点击量 |{hits:^8d}| 总访问者量 |{vistors:^10d}| 总流量 |{fbytes:^15d}|'.format( hits=stat_all['hits'], vistors=len(stat_all['vistors']), fbytes=stat_all['bytes'])) print('=' * 70) print('|2. 状态码分布{:>55}|'.format('')) print('-' * 70) for _status, _cnt in sorted(stat_all['status'].items(), key=lambda x: x[1],
class GeoIP2(GeoIPOO): reader = None cache = {} def __init__(self, path_to_db): from geoip2.database import Reader try: self.reader = Reader(path_to_db) except: pass self.cache = {} def data(self, ip): try: return self.cache[ip] except KeyError: try: response = self.reader.city(ip) except: return None self.cache[ip] = response return response def country(self, ip, default=None): rsp = self.data(ip) if rsp is None: return default try: return rsp.country.iso_code except: return default def country_name(self, ip, default=None): rsp = self.data(ip) if rsp is None: return default try: return rsp.country.name except: return default def region_name(self, ip, default=None): rsp = self.data(ip) if rsp is None: return default try: return rsp.subdivisions.most_specific.name except: return default def city_name(self, ip, default=None): rsp = self.data(ip) if rsp is None: return default try: return rsp.city.name except: return default def postal_code(self, ip, default=None): rsp = self.data(ip) if rsp is None: return default try: return rsp.postal.code except: return default def latitude(self, ip, default=None): rsp = self.data(ip) if rsp is None: return default try: return rsp.location.latitude except: return default def longitude(self, ip, default=None): rsp = self.data(ip) if rsp is None: return default try: return rsp.location.longitude except: return default def close(self): self.reader.close()