1010
1111# MaxMind-DB-Writer-python
1212
13- Make ` mmdb ` format ip library file which can be read by [ ` maxmind ` official language reader] ( https://dev.maxmind.com/geoip/geoip2/downloadable/ )
13+ Make ` mmdb ` format ip library file which can be read by [
14+ ` maxmind ` official language reader] ( https://dev.maxmind.com/geoip/geoip2/downloadable/ )
1415
15- ~~ [ The official perl writer] ( https://github.com/maxmind/MaxMind-DB-Writer-perl ) was written in perl,
16- which was difficult to customize.
16+ ~~ [ The official perl writer] ( https://github.com/maxmind/MaxMind-DB-Writer-perl ) was written in perl,
17+ which was difficult to customize.
1718So I implemented the ` MaxmindDB format ` ip library in python language.~~
1819
19- MaxMind has now released an official Go version of the MMDB writer.
20- If you prefer using Go, you can check out the official Go implementation [ mmdbwriter] ( https://github.com/maxmind/mmdbwriter ) .
20+ MaxMind has now released an official Go version of the MMDB writer.
21+ If you prefer using Go, you can check out the official Go
22+ implementation [ mmdbwriter] ( https://github.com/maxmind/mmdbwriter ) .
2123This project still provides a Python alternative for those who need it.
2224
2325## Install
@@ -27,30 +29,35 @@ pip install -U mmdb_writer
2729```
2830
2931## Usage
32+
3033``` python
3134from netaddr import IPSet
3235
3336from mmdb_writer import MMDBWriter
37+
3438writer = MMDBWriter()
3539
3640writer.insert_network(IPSet([' 1.1.0.0/24' , ' 1.1.1.0/24' ]), {' country' : ' COUNTRY' , ' isp' : ' ISP' })
3741writer.to_db_file(' test.mmdb' )
3842
3943import maxminddb
44+
4045m = maxminddb.open_database(' test.mmdb' )
4146r = m.get(' 1.1.1.1' )
4247assert r == {' country' : ' COUNTRY' , ' isp' : ' ISP' }
4348```
4449
4550## Examples
51+
4652see [ csv_to_mmdb.py] ( ./examples/csv_to_mmdb.py )
4753Here is a professional and clear translation of the README.md section from Chinese into English:
4854
4955## Using the Java Client
5056
51- ### TLDR
57+ If you are using the Java client, you need to be careful to set the ` int_type ` parameter so that Java correctly
58+ recognizes the integer type in the MMDB file.
5259
53- When generating an MMDB file for use with the Java client, you must specify the ` int_type ` :
60+ Example :
5461
5562``` python
5663from mmdb_writer import MMDBWriter
@@ -65,15 +72,15 @@ Alternatively, you can explicitly specify data types using the [Type Enforcement
6572In Java, when deserializing to a structure, the numeric types will use the original MMDB numeric types. The specific
6673conversion relationships are as follows:
6774
68- | mmdb type | java type |
69- | -------------- | ------------|
70- | float (15) | Float |
71- | double (3) | Double |
72- | int32 (8) | Integer |
73- | uint16 (5) | Integer |
74- | uint32 (6) | Long |
75- | uint64 (9) | BigInteger |
76- | uint128 (10) | BigInteger |
75+ | mmdb type | java type |
76+ | -----------| ------------|
77+ | float | Float |
78+ | double | Double |
79+ | int32 | Integer |
80+ | uint16 | Integer |
81+ | uint32 | Long |
82+ | uint64 | BigInteger |
83+ | uint128 | BigInteger |
7784
7885When using the Python writer to generate an MMDB file, by default, it converts integers to the corresponding MMDB type
7986based on the size of the ` int ` . For instance, ` int(1) ` would convert to ` uint16 ` , and ` int(2**16+1) ` would convert
@@ -97,7 +104,17 @@ MMDB file. The behaviors for different `int_type` settings are:
97104| u64 | Stores all integer types as ` uint64 ` . |
98105| u128 | Stores all integer types as ` uint128 ` . |
99106
107+ If you want to use different int types for different scenarios, you can use type wrapping:
108+
109+ ``` python
110+ from mmdb_writer import MMDBWriter, MmdbI32, MmdbF32
111+
112+ writer = MMDBWriter()
113+ # the value of field "i32" will be stored as int32 type
114+ writer.insert_network(IPSet([" 1.0.0.0/24" ]), {" i32" : MmdbI32(128 ), " f32" : MmdbF32(1.22 )})
115+ ```
116+
117+ ## Reference:
100118
101- ## Reference:
102119- [ MaxmindDB format] ( http://maxmind.github.io/MaxMind-DB/ )
103120- [ geoip-mmdb] ( https://github.com/i-rinat/geoip-mmdb )
0 commit comments