country-geoip
A small Python 3.12 package for local IPv4/IPv6 country lookups.
Installation
Install the package from PyPI with uv:
uv add country-geoip
The runtime performs no network requests. A separate command downloads the five Regional Internet Registry (RIR) delegated-statistics files and compiles them into an indexed SQLite database.
Build the database
Build or refresh the local database with the installed command:
country-geoip-build --output country-ip.db
When running directly from the repository, the equivalent command is
python -m country_geoip.builder --output country-ip.db.
The source files are updated regularly, so rebuild the database periodically (weekly is normally sufficient for this use case). The builder writes to a temporary file and atomically replaces the old database only after a successful build. Restart long-running application workers after replacing the database so their read-only connections reopen the new file.
Look up addresses
from country_geoip import CountryDatabase
geoip = CountryDatabase("country-ip.db")
assert geoip.lookup("8.8.8.8") == "US"
print(geoip.lookup("2001:4860:4860::8888"))
The result is an uppercase two-letter country code. Invalid, private,
loopback, multicast and unknown addresses return None.
There is also a command-line interface:
country-geoip --database country-ip.db 8.8.8.8
Output: US (United States)
The command displays both the ISO code and the full country name. The Python API continues to return the two-letter code.
What “country” means
This database uses the public NRO delegated statistics format. Its country is the country associated with the address allocation. It is a simple and useful approximation, not the physical location of a device. Measurement research has found this allocation country consistent with the measured country of use for roughly 98% of examined prefixes. This package has not been independently benchmarked, and accuracy varies by network and registry.
This approach intentionally has no API key, account, or runtime network dependency. A commercial or actively curated geolocation feed can be more accurate for internationally moved ranges, cloud providers and anycast networks.
Example usage
examples/basic_usage.pyshows a lookup and a simple country check.
Development
Run the tests with:
python -m unittest discover -v
Metadata
Release files for country-geoip 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| country_geoip-0.1.0.tar.gz | 6.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| country_geoip-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.3 kB
Release files / country_geoip-0.1.0.tar.gz
| Download URL | country_geoip-0.1.0.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
084e83361471fb8af68ab25c2e7be19ee2d1045b79eabceb15a85ddb3bd86508
|
|
BLAKE2b-256 checksum How to use checksums |
acd87bd600216b6f750c9e96cf2171e1b5b91e18f470762ed21be94e066b0402
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / country_geoip-0.1.0-py3-none-any.whl
| Download URL | country_geoip-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
09f72d2c92dd4c57cfe432622fe52b5b10234012986abd21a8220c73f786f52e
|
|
BLAKE2b-256 checksum How to use checksums |
0feee2fb85aaa1e53f6c866451c05ee6512a295399074e45b5e3f6ddf24ec290
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|