2010-03-26 20:14:53 +00:00
|
|
|
.. _ref-geoip:
|
|
|
|
|
|
|
|
======================
|
|
|
|
Geolocation with GeoIP
|
|
|
|
======================
|
|
|
|
|
2011-09-10 18:04:27 +00:00
|
|
|
.. module:: django.contrib.gis.geoip
|
2010-03-26 20:14:53 +00:00
|
|
|
:synopsis: High-level Python interface for MaxMind's GeoIP C library.
|
|
|
|
|
|
|
|
The :class:`GeoIP` object is a ctypes wrapper for the
|
2013-05-24 16:35:20 +00:00
|
|
|
`MaxMind GeoIP C API`__. [#]_
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
In order to perform IP-based geolocation, the :class:`GeoIP` object requires
|
2012-12-24 21:57:49 +00:00
|
|
|
the GeoIP C libary and either the GeoIP `Country`__ or `City`__
|
|
|
|
datasets in binary format (the CSV files will not work!). These datasets may be
|
2012-10-05 21:14:56 +00:00
|
|
|
`downloaded from MaxMind`__. Grab the ``GeoLiteCountry/GeoIP.dat.gz`` and
|
|
|
|
``GeoLiteCity.dat.gz`` files and unzip them in a directory corresponding to what
|
|
|
|
you set :setting:`GEOIP_PATH` with in your settings. See the example and
|
|
|
|
reference below for more details.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
__ http://www.maxmind.com/app/c
|
|
|
|
__ http://www.maxmind.com/app/country
|
|
|
|
__ http://www.maxmind.com/app/city
|
|
|
|
__ http://www.maxmind.com/download/geoip/database/
|
|
|
|
|
|
|
|
Example
|
|
|
|
=======
|
|
|
|
|
|
|
|
Assuming you have the GeoIP C library installed, here is an example of its
|
|
|
|
usage::
|
|
|
|
|
2012-04-11 21:35:08 +00:00
|
|
|
>>> from django.contrib.gis.geoip import GeoIP
|
2010-03-26 20:14:53 +00:00
|
|
|
>>> g = GeoIP()
|
|
|
|
>>> g.country('google.com')
|
|
|
|
{'country_code': 'US', 'country_name': 'United States'}
|
|
|
|
>>> g.city('72.14.207.99')
|
|
|
|
{'area_code': 650,
|
|
|
|
'city': 'Mountain View',
|
|
|
|
'country_code': 'US',
|
|
|
|
'country_code3': 'USA',
|
|
|
|
'country_name': 'United States',
|
|
|
|
'dma_code': 807,
|
|
|
|
'latitude': 37.419200897216797,
|
|
|
|
'longitude': -122.05740356445312,
|
|
|
|
'postal_code': '94043',
|
|
|
|
'region': 'CA'}
|
|
|
|
>>> g.lat_lon('salon.com')
|
|
|
|
(37.789798736572266, -122.39420318603516)
|
|
|
|
>>> g.lon_lat('uh.edu')
|
2012-12-24 21:57:49 +00:00
|
|
|
(-95.415199279785156, 29.77549934387207)
|
2010-03-26 20:14:53 +00:00
|
|
|
>>> g.geos('24.124.1.80').wkt
|
|
|
|
'POINT (-95.2087020874023438 39.0392990112304688)'
|
|
|
|
|
|
|
|
``GeoIP`` Settings
|
|
|
|
==================
|
|
|
|
|
|
|
|
.. setting:: GEOIP_PATH
|
|
|
|
|
|
|
|
GEOIP_PATH
|
|
|
|
----------
|
|
|
|
|
|
|
|
A string specifying the directory where the GeoIP data files are
|
|
|
|
located. This setting is *required* unless manually specified
|
|
|
|
with ``path`` keyword when initializing the :class:`GeoIP` object.
|
|
|
|
|
|
|
|
.. setting:: GEOIP_LIBRARY_PATH
|
|
|
|
|
|
|
|
GEOIP_LIBRARY_PATH
|
|
|
|
------------------
|
|
|
|
|
|
|
|
A string specifying the location of the GeoIP C library. Typically,
|
|
|
|
this setting is only used if the GeoIP C library is in a non-standard
|
|
|
|
location (e.g., ``/home/sue/lib/libGeoIP.so``).
|
|
|
|
|
|
|
|
.. setting:: GEOIP_COUNTRY
|
|
|
|
|
|
|
|
GEOIP_COUNTRY
|
|
|
|
-------------
|
|
|
|
|
|
|
|
The basename to use for the GeoIP country data file.
|
|
|
|
Defaults to ``'GeoIP.dat'``.
|
|
|
|
|
|
|
|
.. setting:: GEOIP_CITY
|
|
|
|
|
|
|
|
GEOIP_CITY
|
|
|
|
----------
|
|
|
|
|
|
|
|
The basename to use for the GeoIP city data file.
|
|
|
|
Defaults to ``'GeoLiteCity.dat'``.
|
|
|
|
|
|
|
|
``GeoIP`` API
|
|
|
|
=============
|
|
|
|
|
|
|
|
.. class:: GeoIP([path=None, cache=0, country=None, city=None])
|
|
|
|
|
2012-12-24 21:57:49 +00:00
|
|
|
The ``GeoIP`` object does not require any parameters to use the default
|
2010-03-26 20:14:53 +00:00
|
|
|
settings. However, at the very least the :setting:`GEOIP_PATH` setting
|
2012-12-24 21:57:49 +00:00
|
|
|
should be set with the path of the location of your GeoIP data sets. The
|
|
|
|
following intialization keywords may be used to customize any of the
|
|
|
|
defaults.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
=================== =======================================================
|
|
|
|
Keyword Arguments Description
|
|
|
|
=================== =======================================================
|
2012-12-24 21:57:49 +00:00
|
|
|
``path`` Base directory to where GeoIP data is located or the
|
|
|
|
full path to where the city or country data files
|
|
|
|
(.dat) are located. Assumes that both the city and
|
|
|
|
country data sets are located in this directory;
|
2010-03-26 20:14:53 +00:00
|
|
|
overrides the :setting:`GEOIP_PATH` settings attribute.
|
|
|
|
|
|
|
|
``cache`` The cache settings when opening up the GeoIP datasets,
|
|
|
|
and may be an integer in (0, 1, 2, 4) corresponding to
|
2012-12-24 21:57:49 +00:00
|
|
|
the ``GEOIP_STANDARD``, ``GEOIP_MEMORY_CACHE``,
|
|
|
|
``GEOIP_CHECK_CACHE``, and ``GEOIP_INDEX_CACHE``
|
|
|
|
``GeoIPOptions`` C API settings, respectively.
|
2010-03-26 20:14:53 +00:00
|
|
|
Defaults to 0 (``GEOIP_STANDARD``).
|
2012-12-24 21:57:49 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
``country`` The name of the GeoIP country data file. Defaults
|
2012-12-24 21:57:49 +00:00
|
|
|
to ``GeoIP.dat``. Setting this keyword overrides the
|
2010-03-26 20:14:53 +00:00
|
|
|
:setting:`GEOIP_COUNTRY` settings attribute.
|
|
|
|
|
|
|
|
``city`` The name of the GeoIP city data file. Defaults to
|
|
|
|
``GeoLiteCity.dat``. Setting this keyword overrides
|
|
|
|
the :setting:`GEOIP_CITY` settings attribute.
|
|
|
|
=================== =======================================================
|
|
|
|
|
|
|
|
``GeoIP`` Methods
|
|
|
|
=================
|
|
|
|
|
|
|
|
Querying
|
|
|
|
--------
|
|
|
|
|
|
|
|
All the following querying routines may take either a string IP address
|
2012-12-24 21:57:49 +00:00
|
|
|
or a fully qualified domain name (FQDN). For example, both
|
|
|
|
``'205.186.163.125'`` and ``'djangoproject.com'`` would be valid query
|
|
|
|
parameters.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
.. method:: GeoIP.city(query)
|
|
|
|
|
|
|
|
Returns a dictionary of city information for the given query. Some
|
|
|
|
of the values in the dictionary may be undefined (``None``).
|
|
|
|
|
2011-09-10 18:04:27 +00:00
|
|
|
.. method:: GeoIP.country(query)
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-12-24 21:57:49 +00:00
|
|
|
Returns a dictionary with the country code and country for the given
|
2010-03-26 20:14:53 +00:00
|
|
|
query.
|
|
|
|
|
|
|
|
.. method:: GeoIP.country_code(query)
|
|
|
|
|
|
|
|
Returns only the country code corresponding to the query.
|
|
|
|
|
|
|
|
.. method:: GeoIP.country_name(query)
|
|
|
|
|
|
|
|
Returns only the country name corresponding to the query.
|
|
|
|
|
|
|
|
Coordinate Retrieval
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
.. method:: GeoIP.coords(query)
|
|
|
|
|
|
|
|
Returns a coordinate tuple of (longitude, latitude).
|
|
|
|
|
|
|
|
.. method:: GeoIP.lon_lat(query)
|
|
|
|
|
|
|
|
Returns a coordinate tuple of (longitude, latitude).
|
|
|
|
|
|
|
|
.. method:: GeoIP.lat_lon(query)
|
|
|
|
|
|
|
|
Returns a coordinate tuple of (latitude, longitude),
|
|
|
|
|
|
|
|
.. method:: GeoIP.geos(query)
|
|
|
|
|
|
|
|
Returns a :class:`django.contrib.gis.geos.Point` object corresponding to the query.
|
|
|
|
|
|
|
|
Database Information
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
.. attribute:: GeoIP.country_info
|
|
|
|
|
|
|
|
This property returns information about the GeoIP country database.
|
|
|
|
|
|
|
|
.. attribute:: GeoIP.city_info
|
|
|
|
|
|
|
|
This property returns information about the GeoIP city database.
|
|
|
|
|
|
|
|
.. attribute:: GeoIP.info
|
|
|
|
|
|
|
|
This property returns information about all GeoIP databases (both city
|
2011-09-10 18:04:27 +00:00
|
|
|
and country), and the version of the GeoIP C library (if supported).
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
GeoIP-Python API compatibility methods
|
|
|
|
----------------------------------------
|
|
|
|
|
2012-12-24 21:57:49 +00:00
|
|
|
These methods exist to ease compatibility with any code using MaxMind's
|
2010-03-26 20:14:53 +00:00
|
|
|
existing Python API.
|
|
|
|
|
|
|
|
.. classmethod:: GeoIP.open(path, cache)
|
|
|
|
|
|
|
|
This classmethod instantiates the GeoIP object from the given database path
|
|
|
|
and given cache setting.
|
|
|
|
|
2010-05-04 21:49:27 +00:00
|
|
|
.. method:: GeoIP.region_by_addr(query)
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
.. method:: GeoIP.region_by_name(query)
|
|
|
|
|
|
|
|
.. method:: GeoIP.record_by_addr(query)
|
|
|
|
|
|
|
|
.. method:: GeoIP.record_by_name(query)
|
|
|
|
|
|
|
|
.. method:: GeoIP.country_code_by_addr(query)
|
|
|
|
|
|
|
|
.. method:: GeoIP.country_code_by_name(query)
|
|
|
|
|
|
|
|
.. method:: GeoIP.country_name_by_addr(query)
|
|
|
|
|
|
|
|
.. method:: GeoIP.country_name_by_name(query)
|
|
|
|
|
|
|
|
.. rubric:: Footnotes
|
|
|
|
.. [#] GeoIP(R) is a registered trademark of MaxMind, LLC of Boston, Massachusetts.
|