1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
=========================
MaxMind GeoIP2 Python API
=========================
Description
-----------
This package provides an API for the GeoIP2 `web services
<http://dev.maxmind.com/geoip/geoip2/web-services>`_ and `databases
<http://dev.maxmind.com/geoip/geoip2/downloadable>`_. The API also works with
MaxMind's free `GeoLite2 databases
<http://dev.maxmind.com/geoip/geoip2/geolite2/>`_.
Installation
------------
To install the ``geoip2`` module, type:
.. code-block:: bash
$ pip install geoip2
If you are not able to use pip, you may also use easy_install from the
source directory:
.. code-block:: bash
$ 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 <https://github.com/maxmind/libmaxminddb>`_.
Please `see the instructions distributed with it
<https://github.com/maxmind/libmaxminddb/blob/master/README.md>`_.
IP Geolocation Usage
--------------------
IP geolocation is inherently imprecise. Locations are often near the center of
the population. Any location provided by a GeoIP2 database or web service
should not be used to identify a particular address or household.
Usage
-----
To use this API, you first create either a web service object with your
MaxMind ``account_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
-------------------
.. code-block:: pycon
>>> import geoip2.webservice
>>>
>>> # This creates a Client object that can be reused across requests.
>>> # Replace "42" with your account 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
^^^^^^^^^^^^^
.. code-block:: pycon
>>> 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()
Anonymous IP Database
^^^^^^^^^^^^^^^^^^^^^
.. code-block:: pycon
>>> 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-Anonymous-IP.mmdb')
>>>
>>> response = reader.anonymous_ip('85.25.43.84')
>>>
>>> response.is_anonymous
True
>>> response.is_anonymous_vpn
False
>>> response.is_hosting_provider
False
>>> response.is_public_proxy
False
>>> response.is_tor_exit_node
True
>>> response.ip_address
'128.101.101.101'
>>> reader.close()
ASN Database
^^^^^^^^^^^^
.. code-block:: pycon
>>> import geoip2.database
>>>
>>> # This creates a Reader object. You should use the same object
>>> # across multiple requests as creation of it is expensive.
>>> with geoip2.database.Reader('/path/to/GeoLite2-ASN.mmdb') as reader:
>>> response = reader.asn('1.128.0.0')
>>> response.autonomous_system_number
1221
>>> response.autonomous_system_organization
'Telstra Pty Ltd'
Connection-Type Database
^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: pycon
>>> 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
^^^^^^^^^^^^^^^
.. code-block:: pycon
>>> 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()
Enterprise Database
^^^^^^^^^^^^^^^^^^^
.. code-block:: pycon
>>> import geoip2.database
>>>
>>> # This creates a Reader object. You should use the same object
>>> # across multiple requests as creation of it is expensive.
>>> with geoip2.database.Reader('/path/to/GeoIP2-Enterprise.mmdb') as reader:
>>>
>>> # Use the .enterprise method to do a lookup in the Enterprise database
>>> response = reader.enterprise('128.101.101.101')
>>>
>>> response.country.confidence
99
>>> 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.subdivisions.most_specific.confidence
77
>>>
>>> response.city.name
'Minneapolis'
>>> response.country.confidence
11
>>>
>>> response.postal.code
'55455'
>>>
>>> response.location.accuracy_radius
50
>>> response.location.latitude
44.9733
>>> response.location.longitude
-93.2323
ISP Database
^^^^^^^^^^^^
.. code-block:: pycon
>>> 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, the constructor will
raise a ``FileNotFoundError`` on Python 3 or an ``IOError`` on Python 2.
If the IP address passed to a method is invalid, a ``ValueError`` will be
raised. If the file is invalid or there is a bug in the reader, a
``maxminddb.InvalidDatabaseError`` will be raised with a description of the
problem. If an IP address is not in the database, a ``AddressNotFoundError``
will be raised.
Values to use for Database or Dictionary Keys
---------------------------------------------
**We strongly discourage you from using a value from any ``names`` property as
a key in a database or dictionaries.**
These names may change between releases. Instead we recommend using one of the
following:
* ``geoip2.records.City`` - ``city.geoname_id``
* ``geoip2.records.Continent`` - ``continent.code`` or ``continent.geoname_id``
* ``geoip2.records.Country`` and ``geoip2.records.RepresentedCountry`` - ``country.iso_code`` or ``country.geoname_id``
* ``geoip2.records.subdivision`` - ``subdivision.iso_code`` or ``subdivision.geoname_id``
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 <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
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 <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
<http://www.maxmind.com/en/support>`_ for help.
Requirements
------------
This code requires Python 2.7+ 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 <http://semver.org/>`_.
Support
-------
Please report all issues with this code using the `GitHub issue tracker
<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 contact `MaxMind support
<http://www.maxmind.com/en/support>`_ for assistance.
|