Skip to main content

A small cache layer for IP geolocation info

Project description

ipgeocache

PyPi version Python 3.6|3.7|3.8 PRs Welcome

A small cache layer for IP geolocation info.

import ipgeocache
ipgeocache.get("<some ip address>")

If that IP has been requested before, it returns the information from cache. Else, it requests out to https://ipinfo.io

Installation

Requires python3.6+

To install with pip, run:

pip install ipgeocache

This requires you to get an access token from here, can do 50,000 lookups per month as long as you're using it for non-commercial projects.

After setting the IPINFO_TOKEN environment variable:

>>> import ipgeocache, logzero
>>> ipgeocache.get("8.8.8.8", logger=logzero.logger)["hostname"]
[D 200906 17:56:31 __init__:62] Cache Miss: 8.8.8.8, requesting and writing to /home/sean/.local/share/ipgeocache/8.8.8.8
'dns.google'

>>> ipgeocache.get("8.8.8.8", logger=logzero.logger)["hostname"]
[D 200906 17:56:35 __init__:58] Cache Hit: 8.8.8.8, reading /home/sean/.local/share/ipgeocache/8.8.8.8
'dns.google'

>>> ipgeocache.get("8.8.8.8")
{'ip': '8.8.8.8',
 'hostname': 'dns.google',
 'city': 'Mountain View',
 'region': 'California',
 'country': 'US',
 'loc': '37.4056,-122.0775',
 'org': 'AS15169 Google LLC',
 'postal': '94043',
 'timezone': 'America/Los_Angeles'}

Purpose is to just be a thin wrapper that caches this info, so I don't have to think about it for my random projects.

The full function signature is:

ipgeocache.get(ip_address: str,
              token: Optional[str] = None,
              cache_dir: Optional[str] = None,
              logger: Optional[logging.Logger] = None) -> Dict[str, Any]

    """
    Get geolocation info for an IP address

    optional parameters:
    token: ipinfo token to use, if IPINFO_TOKEN not set as an environment variable
    cache_dir: directory to use for cache, overrides default (XDG_DATA_DIR/ipgeocache) if given
    logger: a logger to send cache hit/miss info out on
    """

To change where this stores IP info, you can set the IPGEOCACHE_DIR environment variable.

CLI

Also installs a basic ipgeocache script, which you can pass an IP address to:

$ ipgeocache --help
Usage: ipgeocache [OPTIONS] IP

  Gets geolocation information for an IP address

  For example: 'ipgeocache 192.30.255.112'

Options:
  --ipinfo-token TEXT  Authentication token to use ipinfo API.
                       Consult https://ipinfo.io/signup

  --json               Print geolocation info as JSON
  --help               Show this message and exit.
$ ipgeocache --json 8.8.8.8
{
    "city": "Mountain View",
    "country": "US",
    "hostname": "dns.google",
    "ip": "8.8.8.8",
    "loc": "37.4056,-122.0775",
    "org": "AS15169 Google LLC",
    "postal": "94043",
    "region": "California",
    "timezone": "America/Los_Angeles"
}

Also accessible like python -m ipgeocache

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipgeocache-0.1.4.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

ipgeocache-0.1.4-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file ipgeocache-0.1.4.tar.gz.

File metadata

  • Download URL: ipgeocache-0.1.4.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for ipgeocache-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5f48e486cfba7f932134377a1716c9aa36463ed56fdd7b890a4dc0b8a9a50c68
MD5 e101cadbfa9d0168884e784b52f81fe2
BLAKE2b-256 27b9c6ddc000a953260811c492f806e469455a92c0567038daa86b9173a876ce

See more details on using hashes here.

File details

Details for the file ipgeocache-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: ipgeocache-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for ipgeocache-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b360f1cbdb416bff1de034c4ea4a29e61c4029cb8051cba9cd7d592919addd85
MD5 542f900a7740092b8f811d96b894fa5a
BLAKE2b-256 582ee5be84cee9f0732555d8eb4512a5219e9768a3f25b587fc71799d75991d4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page