MaxMind GeoIP2 Python API

Description

Currently, this distribution provides an API for the GeoIP2 Precision web services (as documented at http://dev.maxmind.com/geoip/precision).

In the future, this distribution will also provide the same API for the GeoIP2 downloadable databases. These databases have not yet been released as a downloadable product.

See geoip2.webservices.Client for details on the web service client API.

Integration with GeoNames

GeoNames (http://www.geonames.org/) 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 unique 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 at http://www.maxmind.com/en/correction.

If you find some other sort of mistake, like an incorrect spelling, please check the GeoNames site (http://www.geonames.org/) 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 at http://www.maxmind.com/en/support for help.

Python version support

This code requires Python 2.6+ or 3.1+. Older versions are not supported.

Support

Please report all issues with this code using the GitHub issue tracker at https://github.com/maxmind/GeoIP2-python/issues

If you are having an issue with a MaxMind service that is not specific to the client API please see http://www.maxmind.com/en/support for details.

Modules

WebServices Client API

This class provides a client API for all the GeoIP Precision web service’s end points. The end points are Country, City, City/ISP/Org, and Omni. Each end point returns a different set of data about an IP address, with Country returning the least data and Omni the most.

Each web service end point is represented by a different model class, and these model classes in turn contain multiple Record classes. The record classes have attributes which contain data about the IP address.

If the web service does not return a particular piece of data for an IP address, the associated attribute is not populated.

The web service may not return any information for an entire record, in which case all of the attributes for that record class will be empty.

SSL

Requests to the GeoIP Precision web service are always made with SSL.

Usage

The basic API for this class is the same for all of the web service end points. First you create a web service object with your MaxMind user_id and license_key, then you call the method corresponding to a specific end point, 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.

Requirements

This library works on Python 2.6+ and Python 3. It requires that the requests HTTP library is installed. See <http://python-requests.org> for details.

Example

>>> import geoip2.webservices
>>> client = geoip2.webservices.Client(42, 'abcdef123456')
>>> omni = client.omni('24.24.24.24')
>>> country = omni.country
>>> print(country.iso_3166_alpha_2)

Exceptions

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

If the web service returns an explicit error document, this is thrown as a GeoIP2WebServiceError exception. If some other sort of error occurs, this is thrown as a GeoIP2HTTPError. The difference is that the webservice error includes an error message and error code delivered by the web service. The latter 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 a GeoIP2HTTPError.

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

What data is returned?

While many of the end points return the same basic records, the attributes which can be populated vary between end points. In addition, while an end point 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 end point to return a record where some or all of the attributes are unpopulated.

See http://dev.maxmind.com/geoip/precision for details on what data each end point may return.

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

Every record class attribute has a corresponding predicate method so you can check to see if the attribute is set.

class geoip2.webservices.Client(user_id, license_key, host='geoip.maxmind.com', languages=None)

This method creates a new client object.

It accepts the following required arguments:

Parameters:
  • user_id – Your MaxMind User ID.
  • license_key – Your MaxMind license key.

Go to https://www.maxmind.com/en/my_license_key to see your MaxMind User ID and license key.

The following keyword arguments are also accepted:

Parameters:
  • host – The hostname to make a request against. This defaults to “geoip.maxmind.com”. In most cases, you should not need to set this explicitly.
  • languages – This is list of language codes. This argument will be passed onto record classes to use when their name properties are called. The default value is [‘en’].

Details on language handling:

The order of the languages is significant. When a record class has multiple names (country, city, etc.), its name property will return the name in the first language that has one.

Note that the only language which is always present in the GeoIP2 Precision data in “en”. If you do not include this language, the name property may end up returning None even when the record hass an English name.

Currently, the valid list of language codes is:

  • en – English names may still include accented characters if that is the accepted spelling in English. In other words, English does not mean ASCII.
  • ja – Japanese
  • ru – Russian
  • zh-CN – Simplified Chinese.

Passing any other language code will result in an error.

city(ip_address='me')

This method calls the GeoIP2 Precision City endpoint.

Parameters:ip_address – IPv4 or IPv6 address as a string. If no address is provided, the address that the web service is called from will be used.
Returns:geoip2.models.City object
city_isp_org(ip_address='me')

This method calls the GeoIP2 Precision City/ISP/Org endpoint.

Parameters:ip_address – IPv4 or IPv6 address as a string. If no address is provided, the address that the web service is called from will be used.
Returns:geoip2.models.CityISPOrg object
country(ip_address='me')

This method calls the GeoIP2 Country endpoint.

Parameters:ip_address – IPv4 or IPv6 address as a string. If no address is provided, the address that the web service is called from will be used.
Returns:geoip2.models.Country object
omni(ip_address='me')

This method calls the GeoIP2 Precision Omni endpoint.

Parameters:ip_address – IPv4 or IPv6 address as a string. If no address is provided, the address that the web service is called from will be used.
Returns:geoip2.models.Omni object

Models

These classes provide models for the data returned by the GeoIP2 Precision City end point.

The only difference between the City, City/ISP/Org, and Omni model classes is which fields in each record may be populated. See http://dev.maxmind.com/geoip/precision for more details.

class geoip2.models.City(raw_response, languages=None)

Model class for the GeoIP2 Precision City end point

Variables:
  • city – Returns a geoip2.records.City object representing country data for the requested IP address.
  • continent – Returns a geoip2.records.Continent object representing continent data for the requested IP address.
  • country – Returns a geoip2.recordsCountry object representing country data for the requested IP address. This record represents the country where MaxMind believes the IP is located in.
  • location – Returns a geoip2.records.Location object representing country data for the requested IP address.
  • region – Returns a geoip2.records.Region object representing country data for the requested IP address.
  • registered_country – Returns a geoip2.recordsCountry object representing the registered country data for the requested IP address. This record represents the country where the ISP has registered a given IP block in and may differ from the user’s country.
  • traits – Returns a geoip2.records.Traits object representing the traits for the request IP address.
class geoip2.models.CityISPOrg(raw_response, languages=None)

Model class for the GeoIP2 Precision City/ISP/Org end point

Variables:
  • city – Returns a geoip2.records.City object representing country data for the requested IP address.
  • continent – Returns a geoip2.records.Continent object representing continent data for the requested IP address.
  • country – Returns a geoip2.recordsCountry object representing country data for the requested IP address. This record represents the country where MaxMind believes the IP is located in.
  • location – Returns a geoip2.records.Location object representing country data for the requested IP address.
  • region – Returns a geoip2.records.Region object representing country data for the requested IP address.
  • registered_country – Returns a geoip2.recordsCountry object representing the registered country data for the requested IP address. This record represents the country where the ISP has registered a given IP block in and may differ from the user’s country.
  • traits – Returns a geoip2.records.Traits object representing the traits for the request IP address.
class geoip2.models.Country(raw_response, languages=None)

Model class for the GeoIP2 Precision Country end point

This class provides the following methods, each of which returns a record object.

Variables:
  • continent – Returns a geoip2.records.Continent object representing continent data for the requested IP address.
  • country – Returns a geoip2.recordsCountry object representing country data for the requested IP address. This record represents the country where MaxMind believes the IP is located in.
  • registered_country – Returns a geoip2.recordsCountry object representing the registered country data for the requested IP address. This record represents the country where the ISP has registered a given IP block in and may differ from the user’s country.
  • traits – Returns a geoip2.records.Traits object representing the traits for the request IP address.
class geoip2.models.Omni(raw_response, languages=None)

Model class for the GeoIP2 Precision Omni end point

Variables:
  • city – Returns a geoip2.records.City object representing country data for the requested IP address.
  • continent – Returns a geoip2.records.Continent object representing continent data for the requested IP address.
  • country – Returns a geoip2.recordsCountry object representing country data for the requested IP address. This record represents the country where MaxMind believes the IP is located in.
  • location – Returns a geoip2.records.Location object representing country data for the requested IP address.
  • region – Returns a geoip2.records.Region object representing country data for the requested IP address.
  • registered_country – Returns a geoip2.recordsCountry object representing the registered country data for the requested IP address. This record represents the country where the ISP has registered a given IP block in and may differ from the user’s country.
  • traits – Returns a geoip2.records.Traits object representing the traits for the request IP address.

Records

class geoip2.records.City(languages=None, **args)

Contains data for the city record associated with an IP address

This class contains the city-level data associated with an IP address.

This record is returned by all the end points except the Country end point.

Attributes:

Variables:
  • confidence – This returns a value from 0-100 indicating MaxMind’s confidence that the city is correct. This attribute is only available from the Omni end point.
  • geoname_id – This returns a GeoName ID for the city. This attribute is returned by all end points.
  • name – Returns the name of the city based on the languages list passed to the constructor. This attribute is returned by all end points.
  • names – This returns a dictionary where the keys are language codes and the values are names. This attribute is returned by all end points.
class geoip2.records.Continent(languages=None, **args)

Contains data for the continent record associated with an IP address

This class contains the continent-level data associated with an IP address.

This record is returned by all the end points.

Attributes:

Variables:
  • continent_code – This returns a two character continent code like “NA” (North America) or “OC” (Oceania). This attribute is returned by all end points.
  • geoname_id – This returns a GeoName ID for the continent. This attribute is returned by all end points.
  • name – Returns the name of the continent based on the languages list passed to the constructor. This attribute is returned by all end points.
  • names – This returns a dictionary where the keys are language codes and the values are names. This attribute is returned by all end points.
class geoip2.records.Country(languages=None, **args)

Contains data for the country record associated with an IP address

This class contains the country-level data associated with an IP address.

This record is returned by all the end points except the Country end point.

Attributes:

Variables:
  • confidence – This returns a value from 0-100 indicating MaxMind’s confidence that the country is correct. This attribute is only available from the Omni end point.
  • geoname_id – This returns a GeoName ID for the country. This attribute is returned by all end points.
  • iso_3166_1_alpha_2 – This returns the two-character ISO 3166-1 (http://en.wikipedia.org/wiki/ISO_3166-1) alpha code for the country. This attribute is returned by all end points.
  • iso_3166_1_alpha_3 – This returns the three-character ISO 3166-1 (http://en.wikipedia.org/wiki/ISO_3166-1) alpha code for the country. This attribute is returned by all end points.
  • name – Returns the name of the country based on the languages list passed to the constructor. This attribute is returned by all end points.
  • names – This returns a dictionary where the keys are language codes and the values are names. This attribute is returned by all end points.
class geoip2.records.Location(**args)

Contains data for the location record associated with an IP address

This class contains the location data associated with an IP address.

This record is returned by all the end points except the Country end point.

Attributes:

Variables:
  • accuracy_radius – This returns the radius in kilometers around the specified location where the IP address is likely to be. This attribute is only available from the Omni end point.
  • latitude – This returns the latitude of the location as a floating point number. This attribute is returned by all end points except the Country end point.
  • longitude – This returns the longitude of the location as a floating point number. This attribute is returned by all end points except the Country end point.
  • metro_code – This returns the metro code of the location if the location is in the US. MaxMind returns the same metro codes as the Google AdWords API (https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions). This attribute is returned by all end points except the Country end point.
  • postal_code – This returns the postal code of the location. Postal codes are not available for all countries. In some countries, this will only contain part of the postal code. This attribute is returned by all end points except the Country end point.
  • postal_confidence – This returns a value from 0-100 indicating MaxMind’s confidence that the postal code is correct. This attribute is only available from the Omni end point.
  • time_zone – This returns the time zone associated with location, as specified by the IANA Time Zone Database (http://www.iana.org/time-zones), e.g., “America/New_York”. This attribute is returned by all end points except the Country end point.
class geoip2.records.PlaceRecord(languages=None, **args)

All records with `names` subclass PlaceRecord`

class geoip2.records.Record(**args)

All records are subclasses of Record

class geoip2.records.Region(languages=None, **args)

Contains data for the region record associated with an IP address

This class contains the region-level data associated with an IP address.

This record is returned by all the end points except the Country end point.

Attributes:

Variables:
  • confidence – This is a value from 0-100 indicating MaxMind’s confidence that the region is correct. This attribute is only available from the Omni end point.
  • geoname_id – This is a GeoName ID for the region. This attribute is returned by all end points.
  • iso_3166_2 – This is a string up to three characters long contain the region portion of the ISO 3166-2 code (http://en.wikipedia.org/wiki/ISO_3166-2). This attribute is returned by all end points.
  • name – The name of the region based on the languages list passed to the constructor. This attribute is returned by all end points.
  • names – A dictionary where the keys are language codes and the values are names. This attribute is returned by all end points.
class geoip2.records.Traits(languages=None, **args)

Contains data for the traits record associated with an IP address

This class contains the traits data associated with an IP address.

This record is returned by all the end points.

This class has the following attributes:

Variables:
  • autonomous_system_number – This returns the autonomous system number (http://en.wikipedia.org/wiki/Autonomous_system_(Internet)) associated with the IP address. This attribute is only available from the City/ISP/Org and Omni end points.
  • autonomous_system_organization – This returns the organization associated with the registered autonomous system number (http://en.wikipedia.org/wiki/Autonomous_system_(Internet)) for the IP address. This attribute is only available from the City/ISP/Org and Omni end points.
  • domain – This returns the second level domain associated with the IP address. This will be something like “example.com” or “example.co.uk”, not “foo.example.com”. This attribute is only available from the City/ISP/Org and Omni end points.
  • ip_address – This returns the IP address that the data in the model is for. If you performed a “me” lookup against the web service, this will be the externally routable IP address for the system the code is running on. If the system is behind a NAT, this may differ from the IP address locally assigned to it. This attribute is returned by all end points.
  • is_anonymous_proxy – This returns true if the IP is an anonymous proxy. See http://dev.maxmind.com/faq/geoip#anonproxy for further details. This attribute is returned by all end points.
  • is_transparent_proxy – This returns true if the IP is a transparent proxy. This attribute is returned by all end points.
  • isp – This returns the name of the ISP associated the IP address. This attribute is only available from the City/ISP/Org and Omni end points.
  • organization – This returns the name of the organization associated the IP address. This attribute is only available from the City/ISP/Org and Omni end points.
  • user_type – This returns the user type associated with the IP address. This can be one of the following values: * business * cafe * cellular * college * content_delivery_network * government * hosting * library * military * residential * router * school * search_engine_spider * traveler This attribute is only available from the Omni end point.

Errors

exception geoip2.errors.GeoIP2Error

There was a generic error in GeoIP2.

This class represents a generic error. It extends RuntimeError and does not add any additional attributes.

exception geoip2.errors.GeoIP2HTTPError(message, http_status=None, uri=None)

There was an error when making your HTTP request.

This class represents an HTTP transport error. It extends GeoIP2Error and adds attributes of its own.

Variables:
  • http_status – The HTTP status code returned
  • uri – The URI queried
exception geoip2.errors.GeoIP2WebServiceError(message, code=None, http_status=None, uri=None)

The GeoIP2 web service returned an error.

This class represents an error returned by MaxMind’s GeoIP2 Precision web service. It extends GeoIP2HTTPError.

Variables:
  • http_status – The HTTP status code returned
  • uri – The URI queried

Indices and tables

copyright:
  1. 2013 by MaxMind, Inc.
license:

GNU Lesser General Public License v2 or later (LGPLv2+)