How to use the maxminddb.MODE_AUTO function in maxminddb

To help you get started, we’ve selected a few maxminddb 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 getsentry / sentry / src / sentry / utils / geo.py View on Github external
def _init_geoip():
    global geo_by_addr
    try:
        import maxminddb
    except ImportError:
        logger.warning("maxminddb module not available.")
        return

    try:
        geo_db = maxminddb.open_database(geoip_path_mmdb, maxminddb.MODE_AUTO)
    except Exception:
        logger.warning("Error opening GeoIP database: %s" % geoip_path_mmdb)
        return

    def encode_bytes(data):
        if isinstance(data, six.text_type):
            return data.encode("ISO-8859-1")
        return data

    def _geo_by_addr(ip):
        geo = geo_db.get(ip)
        if not geo:
            return

        return {
            "country_code": encode_bytes(geo["country"]["iso_code"]),
github maxmind / GeoIP2-python / geoip2 / database.py View on Github external
    def __init__(self, fileish, locales=None, mode=MODE_AUTO):
        """Create GeoIP2 Reader.

        :param fileish: The string path to the GeoIP2 database, or an existing
          file descriptor pointing to the database. Note that this latter
          usage is only valid when mode is MODE_FD.
        :param locales: This is list of locale codes. This argument will be
          passed on to record classes to use when their name properties are
          called. The default value is ['en'].

          The order of the locales is significant. When a record class has
          multiple names (country, city, etc.), its name property will return
          the name in the first locale that has one.

          Note that the only locale which is always present in the GeoIP2
          data is "en". If you do not include this locale, the name property
          may end up returning None even when the record has an English name.
github kz26 / balise / main.py View on Github external
from flask import Flask, Response, abort, request

import geoip2.database
import geoip2.errors
import maxminddb

import ipaddress
import json
import re

__version__ = '2.0.0'

app = Flask(__name__)
app.config.from_pyfile('settings.cfg')

_open_mode = getattr(maxminddb, app.config['GEOIP2_OPEN_MODE']) | maxminddb.MODE_AUTO
GI2_CITY = geoip2.database.Reader(app.config['GEOIP2_CITY_DB'], mode=_open_mode) 
GI2_ASN = geoip2.database.Reader(app.config['GEOIP2_ASN_DB'], mode=_open_mode) 

@app.route('/')
def lookup_ip(ip_addr):
    try: 
        ip = ipaddress.ip_address(ip_addr)
    except ValueError:
        abort(400)
    try:
        rc = GI2_CITY.city(ip.exploded)
        rasn = GI2_ASN.asn(ip.exploded)
    except geoip2.errors.AddressNotFoundError:
        abort(404)
    data = {}
    data['ip'] = ip.exploded