Git Product home page Git Product logo

geoip2-tools's Introduction

geoip2-tools

Latest Tests CI build status Latest PyPI version Python versions Code Climate Test coverage Requirements Status

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.

  1. Sign up for a Maxmind Geolite2 account
  2. Log in to your Maxmind account
  3. In the menu on the left, navigate to Services > My License Key.
  4. Click Generate new license key.
  5. 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.

geoip2-tools's People

Contributors

btimby avatar nekmo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

geoip2-tools's Issues

Not compatible with multiple processes

  • geoip2-tools version: master
  • Python version: 3.8
  • Operating System: Alpine Linux

Description

I am using this library in a tool and part of the use-case is to run multiple processes. This creates a condition where each process is performing the download in parallel and due to the global file name, these processes "race". i.e. one process deletes the '.tar.gz' file while another process is opening it.

What I Did

My code is simply:

    # Trigger download of geoip database.
    db = GEOIP['city']

Solution

To work around this, I forked your repository and I am using tempfile.mkstemp() to create a "private" file name for each process. This works but is not desirable since it causes the download of the database for each process (and if you run the process again, the database will not be discoverable).

My plan is to introduce a lock file around the download procedure so that the first process (race winner) will perform the download and the remaining processes will wait for the file to be available.

Is this a change you would accept in a PR?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.