Skip to main content

Make `mmdb` format ip library file which can be read by maxmind official language reader

Project description

MaxMind-DB-Writer-python

Make mmdb format ip library file which can be read by maxmind official language reader

The official perl writer was written in perl, which was difficult to customize. So I implemented the MaxmindDB format ip library in python language.

MaxMind has now released an official Go version of the MMDB writer. If you prefer using Go, you can check out the official Go implementation mmdbwriter. This project still provides a Python alternative for those who need it.

Install

pip install -U mmdb_writer

Usage

from netaddr import IPSet

from mmdb_writer import MMDBWriter

writer = MMDBWriter()

writer.insert_network(IPSet(['1.1.0.0/24', '1.1.1.0/24']), {'country': 'COUNTRY', 'isp': 'ISP'})
writer.to_db_file('test.mmdb')

import maxminddb

m = maxminddb.open_database('test.mmdb')
r = m.get('1.1.1.1')
assert r == {'country': 'COUNTRY', 'isp': 'ISP'}

Examples

see csv_to_mmdb.py Here is a professional and clear translation of the README.md section from Chinese into English:

Using the Java Client

If you are using the Java client, you need to be careful to set the int_type parameter so that Java correctly recognizes the integer type in the MMDB file.

Example:

from mmdb_writer import MMDBWriter

writer = MMDBWriter(int_type='i32')

Alternatively, you can explicitly specify data types using the Type Enforcement section.

Underlying Principles

In Java, when deserializing to a structure, the numeric types will use the original MMDB numeric types. The specific conversion relationships are as follows:

mmdb type java type
float Float
double Double
int32 Integer
uint16 Integer
uint32 Long
uint64 BigInteger
uint128 BigInteger

When using the Python writer to generate an MMDB file, by default, it converts integers to the corresponding MMDB type based on the size of the int. For instance, int(1) would convert to uint16, and int(2**16+1) would convert to uint32. This may cause deserialization failures in Java clients. Therefore, it is necessary to specify the int_type parameter when generating MMDB files to define the numeric type accurately.

Type Enforcement

MMDB supports a variety of numeric types such as int32, uint16, uint32, uint64, uint128 for integers, and f32, f64 for floating points, while Python only has one integer type and one float type (actually f64).

Therefore, when generating an MMDB file, you need to specify the int_type parameter to define the numeric type of the MMDB file. The behaviors for different int_type settings are:

int_type Behavior
auto (default) Automatically selects the MMDB numeric type based on the value size.
Rules:
int32 for value < 0
uint16 for 0 <= value < 2^16
uint32 for 2^16 <= value < 2^32
uint64 for 2^32 <= value < 2^64
uint128 for value >= 2^64.
i32 Stores all integer types as int32.
u16 Stores all integer types as uint16.
u32 Stores all integer types as uint32.
u64 Stores all integer types as uint64.
u128 Stores all integer types as uint128.

If you want to use different int types for different scenarios, you can use type wrapping:

from mmdb_writer import MMDBWriter, MmdbI32, MmdbF32

writer = MMDBWriter()
# the value of field "i32" will be stored as int32 type
writer.insert_network(IPSet(["1.0.0.0/24"]), {"i32": MmdbI32(128), "f32": MmdbF32(1.22)})

Reference:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mmdb_writer-0.2.6.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mmdb_writer-0.2.6-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file mmdb_writer-0.2.6.tar.gz.

File metadata

  • Download URL: mmdb_writer-0.2.6.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mmdb_writer-0.2.6.tar.gz
Algorithm Hash digest
SHA256 6e77abcb1e2fa1dc4ac82117d4b7532d7238d518fcd0d904f21bb503dd5209cd
MD5 8c74260506d665eeeabb69d1dc5d94c5
BLAKE2b-256 f73fb9c4520ee4c48ff391a56e80e745d1e4c28c694d3585590330c18e2b556f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mmdb_writer-0.2.6.tar.gz:

Publisher: publish.yml on vimt/MaxMind-DB-Writer-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mmdb_writer-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: mmdb_writer-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mmdb_writer-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 76085bed6fdc692403824ec96dbf4b4c5a141b1bc7b73d4a440cbc369b882161
MD5 902bba7b2ab73cabc3e68234e441edab
BLAKE2b-256 0940b8bf9300f91aa1b992111000e779926e0512a1498ec4217dab041a335d11

See more details on using hashes here.

Provenance

The following attestation bundles were made for mmdb_writer-0.2.6-py3-none-any.whl:

Publisher: publish.yml on vimt/MaxMind-DB-Writer-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page