ip3country
ip3country is a Python library providing a zero-dependency, local, fast, and tiny solution for looking up the two-letter country code associated with an IPv4 address. It bundles its own IP-to-country database, eliminating external dependencies or network calls for lookups. The current version is 0.4.0, and it maintains a moderate release cadence, focusing on stability and minimal overhead.
Warnings
- gotcha The IP-to-country data is bundled directly with the library and is not automatically updated. To get the latest IP mappings, users must explicitly upgrade the `ip3country` library to a newer version when available.
- gotcha The `ip3country` library currently only supports IPv4 addresses. Attempting to pass an IPv6 address string to the `lookup_ip` method will likely result in an error (e.g., `socket.error` or `OSError`) or return `None`.
- gotcha The `Ip3Country()` constructor loads the entire IP database into memory. While fast for subsequent lookups, repeatedly calling the constructor (e.g., inside a loop or for every request) is inefficient and can lead to performance issues or increased memory usage.
Install
-
pip install ip3country
Imports
- Ip3Country
from ip3country import Ip3Country
Quickstart
from ip3country import Ip3Country
# Initialize the lookup instance once. This loads the data in memory.
lookup = Ip3Country()
# Lookup an IPv4 address
ip_address_us = "8.8.8.8"
country_code_us = lookup.lookup_ip(ip_address_us)
print(f"IP {ip_address_us} is in {country_code_us}") # Expected: US
ip_address_local = "127.0.0.1"
country_code_local = lookup.lookup_ip(ip_address_local)
print(f"IP {ip_address_local} is in {country_code_local}") # Expected: None or unknown