MaxMind GeoIP2 Python API

5.2.0 · active · verified Sun Apr 05

The geoip2 Python package provides an API for both MaxMind's GeoIP2 and GeoLite2 web services and local databases. It allows developers to perform IP geolocation lookups, retrieving information such as country, city, and ASN details. The library is actively maintained, with version 5.2.0 being the latest stable release, and follows semantic versioning with a regular release cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates how to perform a geolocation lookup using a local GeoLite2 City database. It also includes an optional section for using the GeoIP2 Web Service, which requires a MaxMind account ID and license key. Remember to download a MaxMind database (e.g., GeoLite2-City.mmdb) and specify its path for the local database example. Reader and Client objects should be initialized once and reused for performance.

import os
import geoip2.database
from geoip2.errors import AddressNotFoundError

# --- Using a local GeoLite2 City database ---
# 1. Download GeoLite2 City database from MaxMind (requires account):
#    https://dev.maxmind.com/geoip/geolite2-free-geolocation-data
# 2. Extract the .mmdb file (e.g., GeoLite2-City.mmdb) and place it in a known directory.

database_path = os.environ.get('GEOLITE2_CITY_DB_PATH', './GeoLite2-City.mmdb')

if not os.path.exists(database_path):
    print(f"Error: GeoLite2 City database not found at {database_path}.")
    print("Please download it from MaxMind and update GEOLITE2_CITY_DB_PATH environment variable or file path.")
else:
    try:
        # Reader objects are expensive to create and should be reused across lookups.
        with geoip2.database.Reader(database_path) as reader:
            ip_address = '8.8.8.8'
            try:
                response = reader.city(ip_address)
                print(f"IP: {ip_address}")
                print(f"  Country: {response.country.name} ({response.country.iso_code})")
                print(f"  City: {response.city.name}")
                print(f"  Location: Latitude {response.location.latitude}, Longitude {response.location.longitude}")
            except AddressNotFoundError:
                print(f"IP address {ip_address} not found in the database.")
            except Exception as e:
                print(f"An error occurred during lookup for {ip_address}: {e}")


# --- Using the GeoIP2 Web Service (requires MaxMind Account ID and License Key) ---
# MAXMIND_ACCOUNT_ID and MAXMIND_LICENSE_KEY should be set as environment variables
account_id = os.environ.get('MAXMIND_ACCOUNT_ID')
license_key = os.environ.get('MAXMIND_LICENSE_KEY')

if account_id and license_key:
    print("\n--- Web Service Lookup ---")
    try:
        # Client objects are also expensive and should be reused.
        with geoip2.webservice.Client(account_id, license_key) as client:
            ip_address_ws = '1.1.1.1'
            try:
                response_ws = client.city(ip_address_ws)
                print(f"IP: {ip_address_ws}")
                print(f"  Country: {response_ws.country.name} ({response_ws.country.iso_code})")
                print(f"  City: {response_ws.city.name}")
                print(f"  Location: Latitude {response_ws.location.latitude}, Longitude {response_ws.location.longitude}")
            except AddressNotFoundError:
                print(f"IP address {ip_address_ws} not found via web service.")
            except Exception as e:
                print(f"An error occurred during web service lookup for {ip_address_ws}: {e}")
    except Exception as e:
        print(f"Error initializing web service client: {e}")
else:
    print("\nSkipping web service example: MAXMIND_ACCOUNT_ID and MAXMIND_LICENSE_KEY environment variables not set.")

view raw JSON →