geoip2-tools
Automatic updates and administration of MaxMind GeoIP2 databases.
To install geoip2-tools, run this command in your terminal:
$ pip install geoip2-tools
This is the preferred method to install geoip2-tools, as it will always install the most recent stable release.
Usage
To use this library you must first obtain a license from Maxmind. It is free for the geolite2 version of the database.
- Sign up for a Maxmind Geolite2 account
- Log in to your Maxmind account
- In the menu on the left, navigate to
Services > My License Key
. - Click
Generate new license key
. - Save your license key in a secure site.
geoip2-tools downloads the latest version of the database and keeps it updated for you. By default every 7 days it is downloaded from the Maxmind servers.
from geoip2_tools.manager import Geoip2DataBaseManager
geoip2_manager = Geoip2DataBaseManager('<license key>')
print(geoip2_manager['country'].reader.country('1.1.1.1').country.name) # Australia
Geoip2-tools has aliases for the city, country, and asn Geolite2 databases. The country database is smaller than city. The city database also contains the countries:
city = geoip2_manager['country'].reader.city('<ip address>')
print(city.city.name)
print(city.country.name)
To obtain the ASN you must use the asn database:
asn = geoip2_manager['country'].reader.asn('<ip address>')
print(asn.autonomous_system_number)
print(asn.autonomous_system_organization)
This library is compatible with multiple processes using a lock during the database download. For Windows with multiple
processes portalocker
is recommended. Install it using pip install portalocker
.