mmdb_writer


Namemmdb_writer JSON
Version 0.2.5 PyPI version JSON
download
home_pageNone
SummaryMake `mmdb` format ip library file which can be read by maxmind official language reader
upload_time2024-10-04 15:28:37
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords mmdb maxmind
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            - [MaxMind-DB-Writer-python](#maxmind-db-writer-python)
    * [Install](#install)
    * [Usage](#usage)
    * [Examples](#examples)
    * [Using the Java Client](#using-the-java-client)
        + [TLDR](#tldr)
        + [Underlying Principles](#underlying-principles)
    * [Type Enforcement](#type-enforcement)
    * [Reference:](#reference-)

# MaxMind-DB-Writer-python

Make `mmdb` format ip library file which can be read by [
`maxmind` official language reader](https://dev.maxmind.com/geoip/geoip2/downloadable/)

~~[The official perl writer](https://github.com/maxmind/MaxMind-DB-Writer-perl) 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](https://github.com/maxmind/mmdbwriter).
This project still provides a Python alternative for those who need it.

## Install

```shell script
pip install -U mmdb_writer
```

## Usage

```python
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](./examples/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:

```python
from mmdb_writer import MMDBWriter

writer = MMDBWriter(int_type='i32')
```

Alternatively, you can explicitly specify data types using the [Type Enforcement](#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. <br/>Rules: <br/>`int32` for value < 0 <br/>`uint16` for 0 <= value < 2^16<br/>`uint32` for 2^16 <= value < 2^32<br/>`uint64` for 2^32 <= value < 2^64<br/> `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:

```python
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:

- [MaxmindDB format](http://maxmind.github.io/MaxMind-DB/)
- [geoip-mmdb](https://github.com/i-rinat/geoip-mmdb)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mmdb_writer",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "mmdb, maxmind",
    "author": null,
    "author_email": "VimT <me@vimt.me>",
    "download_url": "https://files.pythonhosted.org/packages/0c/79/12fa35f85ae96738360dc7c22ef21984d1928a6ddf5d6eefb5bed7bfef02/mmdb_writer-0.2.5.tar.gz",
    "platform": null,
    "description": "- [MaxMind-DB-Writer-python](#maxmind-db-writer-python)\n    * [Install](#install)\n    * [Usage](#usage)\n    * [Examples](#examples)\n    * [Using the Java Client](#using-the-java-client)\n        + [TLDR](#tldr)\n        + [Underlying Principles](#underlying-principles)\n    * [Type Enforcement](#type-enforcement)\n    * [Reference:](#reference-)\n\n# MaxMind-DB-Writer-python\n\nMake `mmdb` format ip library file which can be read by [\n`maxmind` official language reader](https://dev.maxmind.com/geoip/geoip2/downloadable/)\n\n~~[The official perl writer](https://github.com/maxmind/MaxMind-DB-Writer-perl) was written in perl,\nwhich was difficult to customize.\nSo I implemented the `MaxmindDB format` ip library in python language.~~\n\nMaxMind has now released an official Go version of the MMDB writer.\nIf you prefer using Go, you can check out the official Go\nimplementation [mmdbwriter](https://github.com/maxmind/mmdbwriter).\nThis project still provides a Python alternative for those who need it.\n\n## Install\n\n```shell script\npip install -U mmdb_writer\n```\n\n## Usage\n\n```python\nfrom netaddr import IPSet\n\nfrom mmdb_writer import MMDBWriter\n\nwriter = MMDBWriter()\n\nwriter.insert_network(IPSet(['1.1.0.0/24', '1.1.1.0/24']), {'country': 'COUNTRY', 'isp': 'ISP'})\nwriter.to_db_file('test.mmdb')\n\nimport maxminddb\n\nm = maxminddb.open_database('test.mmdb')\nr = m.get('1.1.1.1')\nassert r == {'country': 'COUNTRY', 'isp': 'ISP'}\n```\n\n## Examples\n\nsee [csv_to_mmdb.py](./examples/csv_to_mmdb.py)\nHere is a professional and clear translation of the README.md section from Chinese into English:\n\n## Using the Java Client\n\nIf you are using the Java client, you need to be careful to set the `int_type` parameter so that Java correctly\nrecognizes the integer type in the MMDB file.\n\nExample:\n\n```python\nfrom mmdb_writer import MMDBWriter\n\nwriter = MMDBWriter(int_type='i32')\n```\n\nAlternatively, you can explicitly specify data types using the [Type Enforcement](#type-enforcement) section.\n\n### Underlying Principles\n\nIn Java, when deserializing to a structure, the numeric types will use the original MMDB numeric types. The specific\nconversion relationships are as follows:\n\n| mmdb type | java type  |\n|-----------|------------|\n| float     | Float      |\n| double    | Double     |\n| int32     | Integer    |\n| uint16    | Integer    |\n| uint32    | Long       |\n| uint64    | BigInteger |\n| uint128   | BigInteger |\n\nWhen using the Python writer to generate an MMDB file, by default, it converts integers to the corresponding MMDB type\nbased on the size of the `int`. For instance, `int(1)` would convert to `uint16`, and `int(2**16+1)` would convert\nto `uint32`. This may cause deserialization failures in Java clients. Therefore, it is necessary to specify\nthe `int_type` parameter when generating MMDB files to define the numeric type accurately.\n\n## Type Enforcement\n\nMMDB supports a variety of numeric types such as `int32`, `uint16`, `uint32`, `uint64`, `uint128` for integers,\nand `f32`, `f64` for floating points, while Python only has one integer type and one float type (actually `f64`).\n\nTherefore, when generating an MMDB file, you need to specify the `int_type` parameter to define the numeric type of the\nMMDB file. The behaviors for different `int_type` settings are:\n\n| int_type       | Behavior                                                                                                                                                                                                                                                      |\n|----------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| auto (default) | Automatically selects the MMDB numeric type based on the value size. <br/>Rules: <br/>`int32` for value < 0 <br/>`uint16` for 0 <= value < 2^16<br/>`uint32` for 2^16 <= value < 2^32<br/>`uint64` for 2^32 <= value < 2^64<br/> `uint128` for value >= 2^64. |\n| i32            | Stores all integer types as `int32`.                                                                                                                                                                                                                          |\n| u16            | Stores all integer types as `uint16`.                                                                                                                                                                                                                         |\n| u32            | Stores all integer types as `uint32`.                                                                                                                                                                                                                         |\n| u64            | Stores all integer types as `uint64`.                                                                                                                                                                                                                         |\n| u128           | Stores all integer types as `uint128`.                                                                                                                                                                                                                        |\n\nIf you want to use different int types for different scenarios, you can use type wrapping:\n\n```python\nfrom mmdb_writer import MMDBWriter, MmdbI32, MmdbF32\n\nwriter = MMDBWriter()\n# the value of field \"i32\" will be stored as int32 type\nwriter.insert_network(IPSet([\"1.0.0.0/24\"]), {\"i32\": MmdbI32(128), \"f32\": MmdbF32(1.22)})\n```\n\n## Reference:\n\n- [MaxmindDB format](http://maxmind.github.io/MaxMind-DB/)\n- [geoip-mmdb](https://github.com/i-rinat/geoip-mmdb)\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Make `mmdb` format ip library file which can be read by maxmind official language reader",
    "version": "0.2.5",
    "project_urls": {
        "Home": "https://github.com/vimt/MaxMind-DB-Writer-python",
        "Source": "https://github.com/vimt/MaxMind-DB-Writer-python",
        "Tracker": "https://github.com/vimt/MaxMind-DB-Writer-python/issues"
    },
    "split_keywords": [
        "mmdb",
        " maxmind"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "0c7912fa35f85ae96738360dc7c22ef21984d1928a6ddf5d6eefb5bed7bfef02",
                "md5": "6fa0719bc8547b53d898f0ad0b077e53",
                "sha256": "157dc11ea2e417d6c7c16a660ee6baeba32318451a4961cc907ac7165acdd3ab"
            },
            "downloads": -1,
            "filename": "mmdb_writer-0.2.5.tar.gz",
            "has_sig": false,
            "md5_digest": "6fa0719bc8547b53d898f0ad0b077e53",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 20727,
            "upload_time": "2024-10-04T15:28:37",
            "upload_time_iso_8601": "2024-10-04T15:28:37.701928Z",
            "url": "https://files.pythonhosted.org/packages/0c/79/12fa35f85ae96738360dc7c22ef21984d1928a6ddf5d6eefb5bed7bfef02/mmdb_writer-0.2.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-04 15:28:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vimt",
    "github_project": "MaxMind-DB-Writer-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "mmdb_writer"
}
        
Elapsed time: 0.31524s