Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

A small cache layer for IP geolocation info

Project description


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

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("", logger=logzero.logger)["hostname"]
[D 200906 17:56:31 __init__:62] Cache Miss:, requesting and writing to /home/sean/.local/share/ipgeocache/

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

>>> ipgeocache.get("")
{'ip': '',
 'hostname': '',
 '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

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'

  --ipinfo-token TEXT  Authentication token to use ipinfo API.

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

Also accessible like python -m ipgeocache


Requires python3.6+

To install with pip, run:

pip install git+

Project details

Download files

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

Files for ipgeocache, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size ipgeocache-0.1.0-py3-none-any.whl (5.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size ipgeocache-0.1.0.tar.gz (5.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page