ip3country

0.4.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

Initialize the `Ip3Country` class once to load its local IP-to-country database into memory. Then, use the `lookup_ip` method to retrieve the two-letter country code for a given IPv4 address string.

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

view raw JSON →