GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - maxmind/MaxMind-DB-Reader-python: Python MaxMind DB reader extension

BranchesTags
Open more actions menu

Latest commit

 

History

1,410 Commits
1,410 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
dev-bin
dev-bin
 
 
docs
docs
 
 
examples
examples
 
 
extension
extension
 
 
maxminddb
maxminddb
 
 
tests
tests
 
 
.clang-format
.clang-format
 
 
.gitignore
.gitignore
 
 
.gitmodules
.gitmodules
 
 
.pylintrc
.pylintrc
 
 
.readthedocs.yaml
.readthedocs.yaml
 
 
HISTORY.rst
HISTORY.rst
 
 
LICENSE
LICENSE
 
 
MANIFEST.in
MANIFEST.in
 
 
README.rst
README.rst
 
 
lychee.toml
lychee.toml
 
 
mise.lock
mise.lock
 
 
mise.toml
mise.toml
 
 
pyproject.toml
pyproject.toml
 
 
setup.py
setup.py
 
 
uv.lock
uv.lock
 
 

Repository files navigation

MaxMind DB Python Module

Description

This is a Python module for reading MaxMind DB files. The module includes both a pure Python reader and an optional C extension.

MaxMind DB is a binary file format that stores data indexed by IP address subnets (IPv4 or IPv6).

Installation

To install maxminddb, type:

$ pip install maxminddb

If you are not able to install from PyPI, you may also use pip from the source directory:

$ python -m pip install .

The installer will attempt to build the C extension. If this fails, the module will fall-back to the pure Python implementation.

Usage

To use this module, you must first download or create a MaxMind DB file. We provide free GeoLite databases. These files must be decompressed with gunzip.

After you have obtained a database and imported the module, call open_database with a path, or binary file object (with Mode.FD or Mode.AUTO), to the database as the first argument. Optionally, you may pass a mode as the second argument. The modes are available from maxminddb.Mode. Valid modes are:

  • Mode.MMAP_EXT - use the C extension with memory map.
  • Mode.MMAP - read from memory map. Pure Python.
  • Mode.FILE - read database as standard file. Pure Python.
  • Mode.MEMORY - load database into memory. Pure Python.
  • Mode.FD - load database into memory from a binary file object. Pure Python.
  • Mode.AUTO - try Mode.MMAP_EXT, Mode.MMAP, Mode.FILE in that order. A file object is read into memory with the pure Python reader, as with Mode.FD. Pass a path to use the faster C extension. Default.

NOTE: When using a file object, it is the caller's responsibility to be sure that the file object gets closed properly. The caller may close the file object immediately after the Reader object is created. The pure Python reader closes a file descriptor that you pass to it. This includes Mode.AUTO when the C extension is not installed.

The open_database function returns a Reader object. To look up an IP address, use the get method on this object. The method will return the corresponding values for the IP address from the database (e.g., a dictionary for GeoIP/GeoLite databases). If the database does not contain a record for that IP address, the method will return None.

If you wish to also retrieve the prefix length for the record, use the get_with_prefix_len method. This returns a tuple containing the record followed by the network prefix length associated with the record.

You may also iterate over the whole database. The Reader class implements the __iter__ method that returns an iterator. This iterator yields a tuple containing the network and the record.

Example

>>> import maxminddb
>>>
>>> with maxminddb.open_database('GeoLite2-City.mmdb') as reader:
>>>
>>>     reader.get('152.216.7.110')
{'country': ... }
>>>
>>>     reader.get_with_prefix_len('152.216.7.110')
({'country': ... }, 24)
>>>
>>>     for network, record in reader:
>>>         ...

Exceptions

The module will return an InvalidDatabaseError if the database is corrupt or otherwise invalid. A ValueError will be thrown if you look up an invalid IP address or an IPv6 address in an IPv4 database.

The reader also raises InvalidDatabaseError when one record, or the database metadata, exceeds its resource limits: 65,536 decoded values, 512 levels of nesting, or 2 MiB of string and bytes data. Real databases stay far below these limits. Python's recursion limit may stop decoding sooner.

Thread Safety

Both the C extension and pure Python implementations are safe for concurrent reads and support Python 3.13+ free-threading. The C extension provides free-threading support on platforms with pthread support (such as Linux and macOS) and Windows. On other platforms, the extension will use GIL-based protection. Calling close() while reads are in progress may cause exceptions in those threads.

Versioning

The MaxMind DB Python module uses Semantic Versioning.

Support

Please report all issues with this code using the GitHub issue tracker

If you are having an issue with a MaxMind service that is not specific to this API, please contact MaxMind support for assistance.

About

Python MaxMind DB reader extension

maxminddb.readthedocs.org/

Topics

geoipgeoip2maxmindmmdb

Resources

Readme
Apache-2.0 license
Activity
Custom properties

Stars

226 stars

Watchers

18 watching

Forks

42 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.