How to use the geoip2.database.Reader 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 Boerderij / Varken / varken / helpers.py View on Github external
def reader_manager(self, action=None):
        if action == 'open':
            try:
                self.reader = Reader(self.dbfile)
            except FileNotFoundError:
                self.logger.error("Could not find GeoLite2 DB! Downloading!")
                result_status = self.download()
                if result_status:
                    self.logger.error("Could not download GeoLite2 DB!!!, You may need to manually install it.")
                    exit(1)
                else:
                    self.reader = Reader(self.dbfile)
        else:
            self.reader.close()
github duo-labs / cloudmapper / commands / sg_ips.py View on Github external
exit(-1)

    import geoip2.database
    import matplotlib as mpl

    mpl.use("TkAgg")
    import matplotlib.pyplot as plt

    # Used to sort by country
    cidr_dict = {}

    # Locations for graphing
    latlong = {"longitude": [], "latitude": []}

    try:
        asn_reader = geoip2.database.Reader("./data/GeoLite2-ASN.mmdb")
        city_reader = geoip2.database.Reader("./data/GeoLite2-City.mmdb")
    except:
        # geoip files do not exist.  Tell the user.
        print(
            "ERROR: You must download the geoip files GeoLite2-ASN.mmdb and GeoLite2-City.mmdb"
        )
        print(
            "from https://dev.maxmind.com/geoip/geoip2/geolite2/ and put them in ./data/"
        )
        print("\nSteps:")
        print("mkdir -p data; cd data")
        print("\n# Get city data")
        print(
            "curl http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz --output GeoLite2-City.tar.gz"
        )
        print("tar -zxvf GeoLite2-City.tar.gz")
github django / django / django / contrib / gis / geoip2 / base.py View on Github external
country_db = path / (country or GEOIP_SETTINGS['GEOIP_COUNTRY'])
            if country_db.is_file():
                self._country = geoip2.database.Reader(str(country_db), mode=cache)
                self._country_file = country_db

            city_db = path / (city or GEOIP_SETTINGS['GEOIP_CITY'])
            if city_db.is_file():
                self._city = geoip2.database.Reader(str(city_db), mode=cache)
                self._city_file = city_db
            if not self._reader:
                raise GeoIP2Exception('Could not load a database from %s.' % path)
        elif path.is_file():
            # Otherwise, some detective work will be needed to figure out
            # whether the given database path is for the GeoIP country or city
            # databases.
            reader = geoip2.database.Reader(str(path), mode=cache)
            db_type = reader.metadata().database_type

            if db_type.endswith('City'):
                # GeoLite City database detected.
                self._city = reader
                self._city_file = path
            elif db_type.endswith('Country'):
                # GeoIP Country database detected.
                self._country = reader
                self._country_file = path
            else:
                raise GeoIP2Exception('Unable to recognize database edition: %s' % db_type)
        else:
            raise GeoIP2Exception('GeoIP path must be a valid file or directory.')
github zentralopensource / zentral / zentral / core / events / pipeline.py View on Github external
from zentral.conf import settings
from zentral.core.probes.conf import all_probes
from zentral.core.incidents.events import build_incident_events
from zentral.core.incidents.utils import update_or_create_open_incident, update_or_create_open_machine_incident

logger = logging.getLogger('zentral.core.events.pipeline')


city_db_reader = None
try:
    city_db_path = settings["events"]["geoip2_city_db"]
except KeyError:
    pass
else:
    try:
        city_db_reader = geoip2.database.Reader(city_db_path)
    except Exception:
        logger.info("Could not open Geolite2 city database")


def get_city(ip):
    try:
        return city_db_reader.city(ip)
    except Exception:
        pass


def enrich_event(event):
    if isinstance(event, dict):
        event = event_from_event_d(event)
    if event.metadata.request and event.metadata.request.ip and not event.metadata.request.geo and city_db_reader:
        city = get_city(event.metadata.request.ip)
github cbuto / greynoise-visualizer / backend / app.py View on Github external
from flask import Flask, jsonify, url_for, request
import os, requests, ipaddress, geoip2.database
from flask_cors import CORS, cross_origin
from flask_caching import Cache
from collections import Counter
from datetime import datetime
import pandas as pd
from pandas.io.json import json_normalize
import ipaddress

app = Flask(__name__)

CORS(app)
#geoip database reader
reader = geoip2.database.Reader('./GeoLite2-City.mmdb')

#add header (user agent)
headers = {
    'User-Agent': 'Grey Noise Visualizer'
}

#cache setup
#set FLASK_ENV_CONFIG before starting the app
#dev uses simple cache and prod uses redis (docker container)
if("FLASK_ENV_CONFIG" in os.environ and os.environ['FLASK_ENV_CONFIG'] == "prod"):
    cache = Cache(app, config={
        'CACHE_TYPE': 'redis',
        'CACHE_KEY_PREFIX': 'pcache',
        'CACHE_REDIS_HOST': 'redis',
        'CACHE_REDIS_PORT': '6379',
        'CACHE_REDIS_URL': 'redis://redis:6379'
github anilyuk / punydomaincheck / core / domain.py View on Github external
def query_geolocation(self, ip_address):

        try:
            reader = geoip2.database.Reader(GEOLOCATION_DATABASE_FILE)
            response = reader.city(ip_address=str(ip_address))

        except TypeError, e:
            self.logger.warn(e)
            self.logger.warn(ip_address)
            return None
        except IOError, e:
            self.logger.warn("[-] Download GeoIP Database to query geolocation!")
            return None
        else:
            return response
github v3aqb / fwlite / fgfw-lite / util.py View on Github external
except Exception:
        return lst


def sizeof_fmt(num):
    if num < 1024:
        return "%dB" % num
    for x in ['B', 'KB', 'MB', 'GB']:
        if num < 1024.0:
            return "%.1f%s" % (num, x)
        num /= 1024.0
    return "%.1f%s" % (num, 'TB')


try:
    GeoIP2 = geoip2.database.Reader('./fgfw-lite/GeoLite2-Country.mmdb', mode=geoip2.database.MODE_MEMORY) if geoip2 else None
except Exception:
    GeoIP2 = geoip2.database.Reader('./GeoLite2-Country.mmdb', mode=geoip2.database.MODE_MEMORY)


def ip_to_country_code(ip):
    try:
        resp = GeoIP2.country(str(ip))
        return resp.country.iso_code
    except Exception:
        return u''


class ivError(Exception):
    pass
github Antergos / Cnchi / src / geoip.py View on Github external
def _load_database(self, db_path, myip):
        """ Loads cities database """
        try:
            reader = geoip2.database.Reader(db_path)
            self.record = reader.city(myip)
        except maxminddb.errors.InvalidDatabaseError as err:
            logging.error(err)
github django / django / django / contrib / gis / geoip2 / base.py View on Github external
if not path:
            raise GeoIP2Exception('GeoIP path must be provided via parameter or the GEOIP_PATH setting.')

        path = to_path(path)
        if path.is_dir():
            # Constructing the GeoIP database filenames using the settings
            # dictionary. If the database files for the GeoLite country
            # and/or city datasets exist, then try to open them.
            country_db = path / (country or GEOIP_SETTINGS['GEOIP_COUNTRY'])
            if country_db.is_file():
                self._country = geoip2.database.Reader(str(country_db), mode=cache)
                self._country_file = country_db

            city_db = path / (city or GEOIP_SETTINGS['GEOIP_CITY'])
            if city_db.is_file():
                self._city = geoip2.database.Reader(str(city_db), mode=cache)
                self._city_file = city_db
            if not self._reader:
                raise GeoIP2Exception('Could not load a database from %s.' % path)
        elif path.is_file():
            # Otherwise, some detective work will be needed to figure out
            # whether the given database path is for the GeoIP country or city
            # databases.
            reader = geoip2.database.Reader(str(path), mode=cache)
            db_type = reader.metadata().database_type

            if db_type.endswith('City'):
                # GeoLite City database detected.
                self._city = reader
                self._city_file = path
            elif db_type.endswith('Country'):
                # GeoIP Country database detected.
github secynic / ipwhois / ipwhois / examples / elastic_search / elastic_search.py View on Github external
}

        # Set the network country name.
        ret['network']['country_name'] = COUNTRIES[cc]

    except KeyError:

        pass

    try:

        # Get the MaxMind geo data for the query.
        # I do not redistribute the GeoLite2 database, download
        # GeoLite2-City.mmdb from:
        # https://dev.maxmind.com/geoip/geoip2/geolite2/
        mm_reader = geoip2.database.Reader(str(CUR_DIR) +
                                           '/data/GeoLite2-City.mmdb')

        # Query the database.
        mm_response = mm_reader.city(ret['query'])

        # Set the JSON geo data.
        ret['query_geo'] = {
            'lat': mm_response.location.latitude,
            'lon': mm_response.location.longitude
        }
        ret['query_country_name'] = COUNTRIES[mm_response.country.iso_code]

    # Generic exception. Need to determine all raised and update handling.
    # geoip2.errors.AddressNotFoundError, TypeError, etc.
    except Exception as e: