How to use the geoip2.database function in geoip2

To help you get started, we’ve selected a few geoip2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mapsme / omim / tools / download_statistics / resolver.py View on Github external
#!/usr/bin/env python
#coding: utf-8

import geoip2.database
import sys
from collections import defaultdict

reader = geoip2.database.Reader('./GeoLite2-Country.mmdb')

try:
    with sys.stdin as file:
	for rec in file:
	    try:
		parts = rec.strip().split('|')
		ip = parts[0]
		from_country = None
		try:
		    from_country = reader.country(ip).country.name
		except geoip2.errors.AddressNotFoundError:
		    from_country = 'Unknown'
		
		print '{}|{}'.format(from_country,'|'.join(parts))
#		print '{} | {} {} {} | {} | {} | {}'.format(from_country, date[0], date[1], date[2][:4], ip, parts[1][1:13], parts[1][parts[1].find(':')+1:-1])
	    except:
github instacart / lore / lore / transformers.py View on Github external
def __init__(self, column, operator):
        import lore  # This is crazy, why is this statement necessary?
        require(lore.dependencies.GEOIP)
        import geoip2.database

        if GeoIP.reader is None:
            import lore.io
            import glob
            file = lore.io.download(
                'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz',
                cache=True,
                extract=True
            )

            path = [file for file in glob.glob(file.split('.')[0] + '*') if os.path.isdir(file)][0]
            GeoIP.reader = geoip2.database.Reader(os.path.join(path, 'GeoLite2-City.mmdb'))

        super(GeoIP, self).__init__(column)
        self.operator = operator
        self.name += '_' + self.operator
github philhagen / ip2geo / ip2geo.py View on Github external
def check_geoip_files(storagedir):
    try:
        gi1 = geoip2.database.Reader(os.path.join(storagedir, 'GeoLite2-City.mmdb'))
        gi2 = geoip2.database.Reader(os.path.join(storagedir, 'GeoLite2-ASN.mmdb'))
    except IOError:
        sys.stderr.write('ERROR: Required GeoIP files not present. Use "-d" flag to put them into %s.\n' % (storagedir))
        sys.exit(2)

    return (gi1, gi2)
github fportantier / vulpy / old / libsession4.py View on Github external
import json
import base64

import geoip2.database

from cryptography.fernet import Fernet


key = 'JHtM1wEt1I1J9N_Evjwqr3yYauXIqSxYzFnRhcf0ZG0='
fernet = Fernet(key)
ttl = 7200 # seconds
reader = geoip2.database.Reader('GeoLite2-Country.mmdb')


def getcountry(request):

    country = 'XX' # For local connections

    try:
        geo = reader.country(request.remote_addr)
        country = geo.country.iso_code
    except Exception:
        pass

    return country


def create(request, response, username):
github chubin / wttr.in / lib / location.py View on Github external
and basing on this information generates
precise location description.

"""
from __future__ import print_function

import os
import json
import socket
import requests
import geoip2.database

from globals import GEOLITE, GEOLOCATOR_SERVICE, IP2LCACHE, IP2LOCATION_KEY, NOT_FOUND_LOCATION, \
                    ALIASES, BLACKLIST, IATA_CODES_FILE

GEOIP_READER = geoip2.database.Reader(GEOLITE)

def ascii_only(string):
    "Check if `string` contains only ASCII symbols"

    try:
        for _ in range(5):
            string = string.encode('utf-8')
        return True
    except UnicodeDecodeError:
        return False

def is_ip(ip_addr):
    """
    Check if `ip_addr` looks like an IP Address
    """
github securestate / king-phisher / king_phisher / geoip.py View on Github external
:rtype: :py:class:`geoip2.database.Reader`
	"""
	# pylint: disable=global-statement
	global _geoip_db
	if not os.path.isfile(database_file):
		db_path = find.data_file('GeoLite2-City.mmdb')
		if db_path is None:
			raise errors.KingPhisherResourceError('the default geoip database file is unavailable')
		logger.info('initializing the default geoip database')
		shutil.copyfile(db_path, database_file)
	try:
		_geoip_db = geoip2.database.Reader(database_file)
	except maxminddb.errors.InvalidDatabaseError:
		logger.warning('the geoip database file is invalid, downloading a new one')
		download_geolite2_city_db(database_file)
		_geoip_db = geoip2.database.Reader(database_file)
	metadata = _geoip_db.metadata()
	if not metadata.database_type == 'GeoLite2-City':
		raise ValueError('the connected database is not a GeoLite2-City database')
	build_date = datetime.datetime.fromtimestamp(metadata.build_epoch)
	if build_date < datetime.datetime.utcnow() - datetime.timedelta(days=90):
		logger.warning('the geoip database is older than 90 days')
	return _geoip_db
github InQuest / ThreatKB / app / geo_ip_helper.py View on Github external
from app.models import cfg_settings

app.config["GEOIP_ASN_DATABASE_FILE"] = cfg_settings.Cfg_settings.get_setting("GEOIP_ASN_DATABASE_FILE")
if not app.config.get("GEOIP_ASN_DATABASE_FILE"):
    app.config["GEOIP_ASN_DATABASE_FILE"] = os.getenv('GEOIP_ASN_DATABASE_FILE',
                                                      'data/GeoLite2-ASN.mmdb')

app.config["GEOIP_CITY_DATABASE_FILE"] = cfg_settings.Cfg_settings.get_setting("GEOIP_CITY_DATABASE_FILE")
if not app.config.get("GEOIP_CITY_DATABASE_FILE"):
    app.config["GEOIP_CITY_DATABASE_FILE"] = os.getenv('GEOIP_CITY_DATABASE_FILE',
                                                       'data/GeoLite2-City.mmdb')

GEOIP_ASN_DATABASE_FILE = app.config["GEOIP_ASN_DATABASE_FILE"]
GEOIP_CITY_DATABASE_FILE = app.config["GEOIP_CITY_DATABASE_FILE"]
ASN_READER = geoip2.database.Reader(GEOIP_ASN_DATABASE_FILE)
CITY_READER = geoip2.database.Reader(GEOIP_CITY_DATABASE_FILE)
ZIPCODE_DB = pyzipcode.ZipCodeDatabase()


def get_geo_for_ip(ip_address):
    try:
        ip_address = IPAddress(ip_address)
        if not ip_address.is_private:
            asn_info = ASN_READER.asn(ip_address)
            city_info = CITY_READER.city(ip_address)
            zip_code = city_info.postal.code
            return dict(
                ip=ip_address,
                asn=asn_info.autonomous_system_organization,
                country_code=city_info.country.iso_code,
                city=city_info.city.names["en"] if city_info.city.names else None,
                zip_code=zip_code,
github CounterpartyXCP / counterblock / counterblock / lib / modules / counterwallet.py View on Github external
download = True
        else:
            one_week_ago = time.time() - 60 * 60 * 24 * 7
            file_stat = os.stat(geoip_data_path)
            if file_stat.st_ctime < one_week_ago:
                download = True

        if download:
            logger.info("Downloading {}".format(mmdbName))
            # TODO: replace with pythonic way to do this!
            cmd = "cd '{}' && wget -N -q https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz && tar xzf GeoLite2-City.tar.gz && cp */GeoLite2-City.mmdb .".format(config.data_dir)
            util.subprocess_cmd(cmd)
        else:
            logger.info("{} database up to date. Not downloading.".format(mmdbName))
    download_geoip_data()
    module_config['GEOIP'] = geoip2.database.Reader(geoip_data_path)

    if not module_config['SUPPORT_EMAIL']:
        logger.warn("Support email setting not set: To enable, please specify an email for the 'support-email' setting in your counterblockd.conf")
github m-lab / mlab-ns / server / mlabns / util / maxmind.py View on Github external
def get_geo_reader():
    global _maxmind_geo_reader
    if _maxmind_geo_reader is None:
        try:
            # The third parameter is for the "mode", which corresponds to
            # various integer constants in the MaxMind code. 16 corresponds to
            # MODE_FD, which means that we will be passing a file descriptor,
            # not a path to the MM database.
            maxmind_database_file = get_database_file()
            geo_reader = geoip2.database.Reader(maxmind_database_file, None, 16)
        except maxminddb.errors.InvalidDatabaseError:
            logging.error('Invalid MaxMind database file.')
            return GeoRecord()
        except IOError:
            logging.error('Cannot read MaxMind database file.')
            return GeoRecord()

        _maxmind_geo_reader = geo_reader

    return _maxmind_geo_reader
github csirtgadgets / verbose-robot / cif / gatherer / geo.py View on Github external
'/usr/local/share/GeoIP',
    '/usr/local/var/GeoIP',
    '/var/lib/GeoIP'
    './',
]

ENABLE_FQDN = os.getenv('CIF_GATHERER_GEO_FQDN')
CITY_DB_PATH = 'GeoLite2-City.mmdb'
ASN_DB_PATH = 'GeoLite2-ASN.mmdb'
CITY_DB = False

logger = logging.getLogger('cif.gatherer')

for p in DB_SEARCH_PATHS:
    if os.path.isfile(os.path.join(p, CITY_DB_PATH)):
        CITY_DB = geoip2.database.Reader(os.path.join(p, CITY_DB_PATH))

    if os.path.isfile(os.path.join(p, ASN_DB_PATH)):
        ASN_DB = geoip2.database.Reader(os.path.join(p, ASN_DB_PATH))


def _resolve(indicator):
    if not CITY_DB:
        return

    i = indicator.indicator

    if indicator.itype in ['url', 'fqdn']:
        if not ENABLE_FQDN:
            return

        if indicator.itype == 'url':