MaxMind GeoLite2 Python Wrapper

2018.703 · abandoned · verified Thu Apr 16

maxminddb-geolite2 is a Python library that provides convenient access to MaxMind's GeoLite2 City database. It packages a version of the GeoLite2 City database directly within the Python package, aiming to simplify integration by eliminating the need for separate database downloads for basic usage. The latest release, version 2018.703, was published in July 2018.

Common errors

Warnings

Install

Imports

Quickstart

Initialise the geolite2 reader and perform an IP lookup. Remember to close the reader when done to free up resources.

from geolite2 import geolite2

reader = geolite2.reader()
ip_address = '8.8.8.8' # Google Public DNS

# Lookup IP address
geodata = reader.get(ip_address)

if geodata:
    print(f"Geolocation for {ip_address}:")
    print(f"  Country: {geodata.get('country', {}).get('names', {}).get('en')}")
    print(f"  City: {geodata.get('city', {}).get('names', {}).get('en')}")
    print(f"  Latitude: {geodata.get('location', {}).get('latitude')}")
    print(f"  Longitude: {geodata.get('location', {}).get('longitude')}")
else:
    print(f"No geolocation data found for {ip_address}")

# It's important to close the reader to release resources
geolite2.close()

view raw JSON →