• Stars
    star
    173
  • Rank 212,974 (Top 5 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python MaxMind DB reader extension

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 use pip, you may also use easy_install from the source directory:

$ easy_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 GeoLite2 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 file descriptor (in the case of MODE_FD), to the database as the first argument. Optionally, you may pass a mode as the second argument. The modes are exported from maxminddb. 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 file descriptor. Pure Python.
  • MODE_AUTO - try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order. Default.

NOTE: When using MODE_FD, it is the caller's responsibility to be sure that the file descriptor gets closed properly. The caller may close the file descriptor immediately after the Reader object is created.

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 GeoIP2/GeoLite2 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.

Requirements

This code requires Python 3.8+. Older versions are not supported. The C extension requires CPython.

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.

More Repositories

1

GeoIP2-php

PHP API for GeoIP2 webservice client and database reader
PHP
2,279
star
2

GeoIP2-python

Python code for GeoIP2 webservice client and database reader
Python
1,067
star
3

libmaxminddb

C library for the MaxMind DB file format
C
875
star
4

GeoIP2-java

Java API for GeoIP2 webservice client and database reader
Java
748
star
5

geoipupdate

GeoIP update client code
Go
669
star
6

MaxMind-DB-Reader-php

PHP Reader for the MaxMind DB Database Format
PHP
632
star
7

geoip-api-php

DEPRECATED GeoIP Legacy PHP API
PHP
523
star
8

geoip-api-c

DEPRECATED GeoIP Legacy C API
C
369
star
9

GeoIP2-dotnet

MaxMind GeoIP2 .NET API
C#
331
star
10

web-service-common-php

Shared code for the MaxMind Web Service PHP client APIs
PHP
283
star
11

MaxMind-DB

Spec and test data for the MaxMind DB file format
Go
266
star
12

geoipupdate-legacy

GeoIP update client code
C
258
star
13

geoip-api-python

DEPRECATED GeoIP Legacy Python API
C
233
star
14

GeoIP2-node

Node.js API for GeoIP2 webservice client and database reader
TypeScript
205
star
15

geoip2-csv-converter

GeoIP2 CSV Format Converter
Go
198
star
16

geoip-api-java

DEPRECATED GeoIP Legacy Java API
Java
176
star
17

mod_maxminddb

MaxMind DB Apache Module
C
123
star
18

mmdbinspect

look up records for one or more IPs/networks in one or more .mmdb databases
Go
114
star
19

MaxMind-DB-Reader-java

Java reader for the MaxMind DB format
Java
109
star
20

mmdbwriter

Go library for writing MaxMind DB (mmdb) files
Go
100
star
21

MaxMind-DB-Reader-dotnet

.NET Reader for the MaxMind DB Database Format
C#
98
star
22

MaxMind-DB-Writer-perl

Create MaxMind DB database files
Perl
74
star
23

GeoIP2-ruby

Ruby API for GeoIP2 webservice client and database reader
Ruby
55
star
24

minfraud-api-php

PHP API for minFraud Score, Insights, and Factors
PHP
49
star
25

geoip-api-mod_geoip2

DEPRECATED GeoIP Legacy module for Apache 2
C
48
star
26

geoip-api-csharp2

DEPRECATED GeoIP Legacy C# API
C#
47
star
27

MaxMind-DB-Reader-ruby

Ruby reader for the MaxMind DB Database Format
Ruby
45
star
28

getting-started-with-mmdb

A quick guide to writing and reading from your own MMDB databases.
Perl
37
star
29

minfraud-api-python

Python API for minFraud Score, Insights, and Factors
Python
27
star
30

mmdb-from-go-blogpost

Enriching MMDB files with your own data using Go.
Go
23
star
31

ccfd-api-php

Deprecated minFraud Legacy PHP API
PHP
23
star
32

minfraud-api-dotnet

.NET API for MaxMind minFraud Score, Insights, and Factors
C#
19
star
33

GeoIP2-perl

Perl API for MaxMind's GeoIP2 web services and databases
Perl
18
star
34

minfraud-api-java

Java API for minFraud Score, Insights, and Factors
Java
18
star
35

minfraud-api-ruby

Ruby API for minFraud Score, Insights, and Factors
Ruby
14
star
36

mm-geofeed-verifier

Verify the format of a geofeed file, and make some comparisons to data in an MMDB file.
Go
14
star
37

dev-hire-homework

A homework exercise for engineering applicants
Perl
13
star
38

minfraud-api-node

Node.js API for MaxMind minFraud Score, Insights, and Factors
TypeScript
13
star
39

mm-network-analyzer

A program to aid in diagnosing networking issues
Go
12
star
40

MaxMind-DB-Reader-perl

Read MaxMind DB files and look up IP addresses
Perl
12
star
41

mmdbverify

Verifier for the MaxMind DB format
Go
10
star
42

geoip-api-perl

DEPRECATED GeoIP Legacy Perl API
Perl
10
star
43

Stepford

A vaguely Rake/Make/Cake-like thing for Perl - create steps and let a runner run them
Perl
9
star
44

Locale-Country-Multilingual

mapping ISO codes to localized country names
Perl
7
star
45

ccfd-api-java

Deprecated minFraud Legacy Java API
Java
7
star
46

Database-Migrator

Mirror of Database-Migrator on urth.org
Perl
5
star
47

Net-Works

Sane APIs for IP addresses and networks
Perl
5
star
48

MaxMind-DB-Reader-XS

Fast XS implementation of MaxMind DB reader
Perl
5
star
49

ccfd-api-asp

minFraud ASP API
ASP
3
star
50

webservice-paypal-paymentsadvanced

A simple wrapper around the PayPal Payments Advanced web service
Perl
3
star
51

dev-site

Static site generator for https://dev.maxmind.com.
MDX
3
star
52

xgb2code

A converter for xgboost model dumps to code.
Go
3
star
53

geoip-api-mscom

DEPRECATED GeoIP Legacy MS COM API
C
2
star
54

gatling-gen

C++
2
star
55

geolite2-ws-blogpost

Integrating MaxMind's Free and Paid IP Geolocation Web Services (in PHP)
PHP
2
star
56

App-CISetup

Command line tools to generate and update Travis and AppVeyor configs for Perl libraries
Perl
2
star
57

MaxMind-DB-Common-perl

Code shared by the MaxMind DB reader and writer modules
Perl
2
star
58

minfraud-api-perl

Perl API for minFraud Score, Insights, and Factors
Perl
2
star
59

WebService-PivotalTracker

Perl library for the Pivotal Tracker REST API
Perl
2
star
60

fuzzing-workshop

Code for Summit Fuzzing Workshop
Go
1
star
61

Dist-Zilla-PluginBundle-MAXMIND

Perl
1
star
62

TeamCity-Message

Generate TeamCity build messages
Perl
1
star
63

blog-site

Static site generator for https://blog.maxmind.com.
SCSS
1
star
64

api-specs

TypeScript
1
star
65

TAP-Formatter-TeamCity

Emit test results as TeamCity build messages
Perl
1
star