Skip to main content

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

Metadata

Release files for ipgeocache 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipgeocache 0.1.4
File Size Uploaded
ipgeocache-0.1.4.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ipgeocache 0.1.4
File Interpreter ABI Platform
ipgeocache-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / ipgeocache-0.1.4.tar.gz

Download URL ipgeocache-0.1.4.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5f48e486cfba7f932134377a1716c9aa36463ed56fdd7b890a4dc0b8a9a50c68
BLAKE2b-256 checksum
How to use checksums
27b9c6ddc000a953260811c492f806e469455a92c0567038daa86b9173a876ce
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / ipgeocache-0.1.4-py3-none-any.whl

Download URL ipgeocache-0.1.4-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b360f1cbdb416bff1de034c4ea4a29e61c4029cb8051cba9cd7d592919addd85
BLAKE2b-256 checksum
How to use checksums
582ee5be84cee9f0732555d8eb4512a5219e9768a3f25b587fc71799d75991d4
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

1 release file

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page