skip to navigation
skip to content

Not Logged In

geoip2 0.6.0

MaxMind GeoIP2 API

Package Documentation

Latest Version: 2.2.0

Beta Note

This is a beta release. The API may change before the first production release, which will be numbered 2.0.0.

You may find information on the GeoIP2 beta release process on our website.

Description

This package provides an API for the GeoIP2 web services and databases. The API also works with MaxMind’s free GeoLite2 databases.

Installation

To install the geoip2 module, type:

$ pip install geoip2

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

$ easy_install .

Database Reader Extension

If you wish to use the C extension for the database reader, you must first install the libmaxminddb C API. Please see the instructions distributed with it.

Usage

To use this API, you first create either a web service object with your MaxMind user_id and license_key or a database reader object with the path to your database file. After doing this, you may call the method corresponding to request type (e.g., city or country), passing it the IP address you want to look up.

If the request succeeds, the method call will return a model class for the end point you called. This model in turn contains multiple record classes, each of which represents part of the data returned by the web service.

If the request fails, the client class throws an exception.

Web Service Example

>>> import geoip2.webservice
>>>
>>> # This creates a Client object that can be reused across requests.
>>> # Replace "42" with your user ID and "license_key" with your license
>>> # key.
>>> client = geoip2.webservice.Client(42, 'license_key')
>>>
>>> # Replace "insights" with the method corresponding to the web service
>>> # that you are using, e.g., "country", "city".
>>> response = client.insights('128.101.101.101')
>>>
>>> response.country.iso_code
'US'
>>> response.country.name
'United States'
>>> response.country.names['zh-CN']
u'美国'
>>>
>>> response.subdivisions.most_specific.name
'Minnesota'
>>> response.subdivisions.most_specific.iso_code
'MN'
>>>
>>> response.city.name
'Minneapolis'
>>>
>>> response.postal.code
'55455'
>>>
>>> response.location.latitude
44.9733
>>> response.location.longitude
-93.2323

Web Service Client Exceptions

For details on the possible errors returned by the web service itself, see http://dev.maxmind.com/geoip/geoip2/web-services for the GeoIP2 Precision web service docs.

If the web service returns an explicit error document, this is thrown as a AddressNotFoundError, AuthenticationError, InvalidRequestError, or OutOfQueriesError as appropriate. These all subclass GeoIP2Error.

If some other sort of error occurs, this is thrown as an HTTPError. This is thrown when some sort of unanticipated error occurs, such as the web service returning a 500 or an invalid error document. If the web service returns any status code besides 200, 4xx, or 5xx, this also becomes an HTTPError.

Finally, if the web service returns a 200 but the body is invalid, the client throws a GeoIP2Error.

Database Example

City Database

>>> import geoip2.database
>>>
>>> # This creates a Reader object. You should use the same object
>>> # across multiple requests as creation of it is expensive.
>>> reader = geoip2.database.Reader('/path/to/GeoLite2-City.mmdb')
>>>
>>> # Replace "city" with the method corresponding to the database
>>> # that you are using, e.g., "country".
>>> response = reader.city('128.101.101.101')
>>>
>>> response.country.iso_code
'US'
>>> response.country.name
'United States'
>>> response.country.names['zh-CN']
u'美国'
>>>
>>> response.subdivisions.most_specific.name
'Minnesota'
>>> response.subdivisions.most_specific.iso_code
'MN'
>>>
>>> response.city.name
'Minneapolis'
>>>
>>> response.postal.code
'55455'
>>>
>>> response.location.latitude
44.9733
>>> response.location.longitude
-93.2323
>>> reader.close()

Connection-Type Database

>>> import geoip2.database
>>>
>>> # This creates a Reader object. You should use the same object
>>> # across multiple requests as creation of it is expensive.
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-Connection-Type.mmdb')
>>>
>>> response = reader.connection_type('128.101.101.101')
>>>
>>> response.connection_type
'Corporate'
>>> response.ip_address
'128.101.101.101'
>>> reader.close()

Domain Database

>>> import geoip2.database
>>>
>>> # This creates a Reader object. You should use the same object
>>> # across multiple requests as creation of it is expensive.
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-Domain.mmdb')
>>>
>>> response = reader.domain('128.101.101.101')
>>>
>>> response.domain
'umn.edu'
>>> response.ip_address
'128.101.101.101'
>>> reader.close()

ISP Database

>>> import geoip2.database
>>>
>>> # This creates a Reader object. You should use the same object
>>> # across multiple requests as creation of it is expensive.
>>> reader = geoip2.database.Reader('/path/to/GeoIP2-ISP.mmdb')
>>>
>>> response = reader.isp('1.128.0.0')
>>>
>>> response.autonomous_system_number
1221
>>> response.autonomous_system_organization
'Telstra Pty Ltd'
>>> response.isp
'Telstra Internet'
>>> response.organization
'Telstra Internet'
>>> response.ip_address
'128.101.101.101'
>>> reader.close()

Database Reader Exceptions

If the database file does not exist or is not readable, a ValueError will be thrown. If the file is invalid or there is a bug in the reader, a maxminddb.InvalidDatabaseError will be thrown with a description of the problem. If an IP address is not in the database, a AddressNotFoundError exception will be thrown.

What data is returned?

While many of the models contain the same basic records, the attributes which can be populated vary between web service end points or databases. In addition, while a model may offer a particular piece of data, MaxMind does not always have every piece of data for any given IP address.

Because of these factors, it is possible for any request to return a record where some or all of the attributes are unpopulated.

The only piece of data which is always returned is the ip_address attribute in the geoip2.records.Traits record.

Integration with GeoNames

GeoNames offers web services and downloadable databases with data on geographical features around the world, including populated places. They offer both free and paid premium data. Each feature is uniquely identified by a geoname_id, which is an integer.

Many of the records returned by the GeoIP web services and databases include a geoname_id field. This is the ID of a geographical feature (city, region, country, etc.) in the GeoNames database.

Some of the data that MaxMind provides is also sourced from GeoNames. We source things like place names, ISO codes, and other similar data from the GeoNames premium data set.

Reporting Data Problems

If the problem you find is that an IP address is incorrectly mapped, please submit your correction to MaxMind.

If you find some other sort of mistake, like an incorrect spelling, please check the GeoNames site first. Once you’ve searched for a place and found it on the GeoNames map view, there are a number of links you can use to correct data (“move”, “edit”, “alternate names”, etc.). Once the correction is part of the GeoNames data set, it will be automatically incorporated into future MaxMind releases.

If you are a paying MaxMind customer and you’re not sure where to submit a correction, please contact MaxMind support for help.

Requirements

This code requires Python 2.6+ or 3.3+. Older versions are not supported. This library has been tested with CPython and PyPy.

The Requests HTTP library is also required. See <http://python-requests.org> for details.

Versioning

The GeoIP2 Python API 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 the client API, please contact MaxMind support for assistance.

 
File Type Py Version Uploaded on Size
geoip2-0.6.0-py2.py3-none-any.whl (md5) Python Wheel 2.7 2014-07-22 21KB
geoip2-0.6.0.tar.gz (md5) Source 2014-07-22 111KB
  • Downloads (All Versions):
  • 1863 downloads in the last day
  • 13514 downloads in the last week
  • 65035 downloads in the last month