A Python client for the IPLocate.io geolocation API. Look up detailed geolocation and threat intelligence data for any IP address:
- IP geolocation: IP to country, IP to city, IP to region/state, coordinates, timezone, postal code
- ASN information: Internet service provider, network details, routing information
- Privacy & threat detection: VPN, proxy, Tor, hosting provider detection
- Company information: Business details associated with IP addresses - company name, domain, type (ISP/hosting/education/government/business)
- Abuse contact: Network abuse reporting information
- Hosting detection: Cloud provider and hosting service detection using our proprietary hosting detection engine
See what information we can provide for your IP address.
You can make 1,000 free requests per day with a free account. For higher plans, check out API pricing.
pip install python-iplocatefromiplocateimportIPLocateClient# Create a client with your API key# Get your free API key from https://iplocate.io/signupclient=IPLocateClient(api_key="your-api-key")
# Look up an IP addressresult=client.lookup("8.8.8.8")
print(f"IP: {result.ip}")
ifresult.country:
print(f"Country: {result.country}")
ifresult.city:
print(f"City: {result.city}")
# Check privacy flagsprint(f"Is VPN: {result.privacy.is_vpn}")
print(f"Is Proxy: {result.privacy.is_proxy}")importasynciofromiplocateimportAsyncIPLocateClientasyncdefmain():
asyncwithAsyncIPLocateClient(api_key="your-api-key") asclient:
result=awaitclient.lookup("8.8.8.8")
print(f"Country: {result.country}")
asyncio.run(main())# Look up your own IP address (no IP parameter)result=client.lookup()
print(f"Your IP: {result.ip}")result=client.lookup("203.0.113.1")
print(f"Country: {result.country} ({result.country_code})")result=client.lookup("203.0.113.1")
print(f"Currency: {result.currency_code}")result=client.lookup("203.0.113.1")
print(f"Calling code: {result.calling_code}")Get your free API key from IPLocate.io, and pass it when creating the client:
client=IPLocateClient(api_key="your-api-key")fromiplocateimportIPLocateClientclient=IPLocateClient(api_key="your-api-key")
result=client.lookup("203.0.113.1")
print(f"Country: {result.country} ({result.country_code})")
ifresult.latitudeandresult.longitude:
print(f"Coordinates: {result.latitude:.4f}, {result.longitude:.4f}")result=client.lookup("192.0.2.1")
ifresult.privacy.is_vpn:
print("This IP is using a VPN")
ifresult.privacy.is_proxy:
print("This IP is using a proxy")
ifresult.privacy.is_tor:
print("This IP is using Tor")result=client.lookup("8.8.8.8")
ifresult.asn:
print(f"ASN: {result.asn.asn}")
print(f"ISP: {result.asn.name}")
print(f"Network: {result.asn.route}")importipaddressfromiplocateimportIPLocateClientclient=IPLocateClient(api_key="your-api-key")
# String IPresult1=client.lookup("8.8.8.8")
# ipaddress objectsipv4=ipaddress.IPv4Address("8.8.8.8")
result2=client.lookup(ipv4)
ipv6=ipaddress.IPv6Address("2001:4860:4860::8888")
result3=client.lookup(ipv6)importhttpxfromiplocateimportIPLocateClient, AsyncIPLocateClient# Custom timeout and base URLclient=IPLocateClient(
api_key="your-api-key",
timeout=60.0,
base_url="https://custom-endpoint.com/api"
)
# Custom HTTP clientcustom_http_client=httpx.Client(timeout=60.0)
client=IPLocateClient(
api_key="your-api-key",
http_client=custom_http_client
)
# Async with custom clientasync_http_client=httpx.AsyncClient(timeout=60.0)
async_client=AsyncIPLocateClient(
api_key="your-api-key", http_client=async_http_client
)# SynchronouswithIPLocateClient(api_key="your-api-key") asclient:
result=client.lookup("8.8.8.8")
print(result.country)
# AsynchronousasyncwithAsyncIPLocateClient(api_key="your-api-key") asclient:
result=awaitclient.lookup("8.8.8.8")
print(result.country)The LookupResponse object contains all available data:
@dataclassclassLookupResponse:
ip: strprivacy: Privacycountry: Optional[str] =Nonecountry_code: Optional[str] =Noneis_eu: bool=Falsecity: Optional[str] =Nonecontinent: Optional[str] =Nonelatitude: Optional[float] =Nonelongitude: Optional[float] =Nonetime_zone: Optional[str] =Nonepostal_code: Optional[str] =Nonesubdivision: Optional[str] =Nonecurrency_code: Optional[str] =Nonecalling_code: Optional[str] =Nonenetwork: Optional[str] =Noneasn: Optional[ASN] =Nonecompany: Optional[Company] =Nonehosting: Optional[Hosting] =Noneabuse: Optional[Abuse] =NoneFields marked as Optional may be None if data is not available.
fromiplocateimportIPLocateClientfromiplocate.exceptionsimport (
APIError, RateLimitError, AuthenticationError, InvalidIPError
)
client=IPLocateClient(api_key="your-api-key")
try:
result=client.lookup("8.8.8.8")
exceptInvalidIPErrorase:
print(f"Invalid IP address: {e.ip}")
exceptAuthenticationError:
print("Invalid API key")
exceptRateLimitError:
print("Rate limit exceeded")
exceptAPIErrorase:
print(f"API error ({e.status_code}): {e.message}")Common API errors:
InvalidIPError: Invalid IP address formatAuthenticationError: Invalid API key (HTTP 403)NotFoundError: IP address not found (HTTP 404)RateLimitError: Rate limit exceeded (HTTP 429)APIError: Other API errors (HTTP 500, etc.)
The library provides full async support with AsyncIPLocateClient:
importasynciofromiplocateimportAsyncIPLocateClientasyncdeflookup_multiple_ips():
asyncwithAsyncIPLocateClient(api_key="your-api-key") asclient:
# Concurrent lookupstasks= [
client.lookup("8.8.8.8"),
client.lookup("1.1.1.1"),
client.lookup("208.67.222.222")
]
results=awaitasyncio.gather(*tasks)
forresultinresults:
print(f"{result.ip}: {result.country}")
asyncio.run(lookup_multiple_ips())For complete API documentation, visit iplocate.io/docs.
- Python 3.8+
- httpx >= 0.24.0
Install development dependencies:
pip install -e ".[dev]"Run tests:
pytestRun type checking:
mypy iplocateFormat code:
black iplocate tests
isort iplocate testsThis project is licensed under the MIT License - see the LICENSE file for details.
Since 2017, IPLocate has set out to provide the most reliable and accurate IP address data.
We process 50TB+ of data to produce our comprehensive IP geolocation, IP to company, proxy and VPN detection, hosting detection, ASN, and WHOIS data sets. Our API handles over 15 billion requests a month for thousands of businesses and developers.
- Email: support@iplocate.io
- Website: iplocate.io
- Documentation: iplocate.io/docs
- Sign up for a free API Key: iplocate.io/signup