Official Python library for Ipregistry
Project description
Ipregistry Python Client Library
This is the official Python client library for the Ipregistry IP geolocation and threat data API, allowing you to lookup your own IP address or specified ones. Responses return multiple data points including carrier, company, currency, location, timezone, threat information, and more.
Starting version 3 of the library, support for Python 2 has been dropped. Version 5 and above require Python 3.10+.
Getting Started
You'll need an Ipregistry API key, which you can get along with 100,000 free lookups by signing up for a free account at https://ipregistry.co.
Installation
pip install ipregistry
Quick Start
Single IP Lookup
from ipregistry import IpregistryClient
client = IpregistryClient("YOUR_API_KEY")
response = client.lookup_ip("54.85.132.205")
# Printing whole response
print(response)
# Retrieving a specific field
country_code = response.data.location.country.code
# Getting number of credits consumed or remaining
credits_consumed = response.credits.consumed
credits_remaining = response.credits.remaining
Single ASN Lookup
from ipregistry import IpregistryClient
client = IpregistryClient("YOUR_API_KEY")
response = client.lookup_asn(42)
print(response.credits.consumed)
print(response.data.prefixes)
print(response.data.relationships)
Batch IP Lookup
from ipregistry import IpregistryClient
client = IpregistryClient("YOUR_API_KEY")
response = client.batch_lookup_ips(["54.85.132.205", "8.8.8.8", "2001:67c:2e8:22::c100:68b"])
for ip_info in response.data:
print(ip_info)
Origin IP Lookup
from ipregistry import IpregistryClient
client = IpregistryClient("YOUR_API_KEY")
response = client.origin_lookup_ip()
print(response.data)
User-Agent Parsing
from ipregistry import IpregistryClient
client = IpregistryClient("YOUR_API_KEY")
response = client.parse_user_agent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36')
print(response.data)
Asynchronous Client
An asyncio-based client with the same feature set is available when the library is installed
with the async extra (pip install ipregistry[async]):
from ipregistry import AsyncIpregistryClient
async def main():
async with AsyncIpregistryClient("YOUR_API_KEY") as client:
response = await client.lookup_ip("54.85.132.205")
print(response.data.location.country.code)
More advanced examples are available in the samples folder.
Configuration
Timeouts, retries and the User-Agent header are configurable through IpregistryConfig:
from ipregistry import IpregistryClient, IpregistryConfig
config = IpregistryConfig(
"YOUR_API_KEY",
timeout=15, # seconds; a (connect, read) tuple is also accepted
retry_max_attempts=3, # attempts per request, including the initial one
retry_interval=1, # base delay in seconds, doubled on each retry
retry_on_server_error=True, # retry 5xx responses
retry_on_too_many_requests=False, # retry 429 responses, honoring Retry-After
user_agent=None # custom User-Agent header value
)
client = IpregistryClient(config)
Transient network errors are always retried up to retry_max_attempts.
The client reuses pooled HTTP connections through a requests.Session. You may pass your own
session with IpregistryClient("YOUR_API_KEY", session=my_session) for proxy or TLS control,
and release resources with client.close() or by using the client as a context manager.
Batch lookups larger than the API limit of 1024 items are automatically split into concurrent
chunks. Tune this with IpregistryClient("YOUR_API_KEY", max_batch_size=1024, batch_concurrency=4).
Caching
This Ipregistry client library has built-in support for in-memory caching. By default caching is disabled. Below are examples to enable and configure a caching strategy. Once enabled, default cache strategy is to memoize up to 2048 lookups for at most 10min. You can change preferences as follows:
Enabling caching
Enable caching by passing an instance of InMemoryCache:
from ipregistry import InMemoryCache, IpregistryClient
client = IpregistryClient("YOUR_API_KEY", cache=InMemoryCache(maxsize=2048, ttl=600))
Disabling caching
Disable caching by passing an instance of NoCache:
from ipregistry import IpregistryClient, NoCache
client = IpregistryClient("YOUR_API_KEY", cache=NoCache())
European Union Base URL
Using the EU base URL, your requests are handled by the closest cluster of nodes in the European Union:
from ipregistry import IpregistryClient, NoCache
client = IpregistryClient(IpregistryConfig("YOUR_API_KEY").with_eu_base_url())
Errors
All Ipregistry exceptions inherit IpregistryError class.
Main subtypes are ApiError and ClientError. Failed entries in batch responses are instances of IpregistryLookupError (also available under the legacy alias LookupError).
Errors of type ApiError include a code field that maps to the one described in the Ipregistry documentation, along with a typed error_code enum value (ErrorCode) that is None for unrecognized codes.
Filtering bots
You might want to prevent Ipregistry API requests for crawlers or bots browsing your pages.
A manner to proceed is to identify bots using the User-Agent header.
To ease this process, the library includes a utility method:
from ipregistry import UserAgents
is_bot = UserAgents.is_bot('YOUR_USER_AGENT_HEADER_VALUE_HERE')
Other Libraries
There are official Ipregistry client libraries available for many languages including Java, Javascript, and more.
Are you looking for an official client with a programming language or framework we do not support yet? let us know.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ipregistry-5.0.1.tar.gz.
File metadata
- Download URL: ipregistry-5.0.1.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9834d9f61f8f6df0edc2616f23b5c6d5ba8e37ad66f083e332258b4b76c3f360
|
|
| MD5 |
229e9142518973acefecf4ef64208701
|
|
| BLAKE2b-256 |
0e23e6a8be09a8aba526383365d8fb55f54c095411a324641ba800e65607ae26
|
Provenance
The following attestation bundles were made for ipregistry-5.0.1.tar.gz:
Publisher:
release.yaml on ipregistry/ipregistry-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ipregistry-5.0.1.tar.gz -
Subject digest:
9834d9f61f8f6df0edc2616f23b5c6d5ba8e37ad66f083e332258b4b76c3f360 - Sigstore transparency entry: 2125127095
- Sigstore integration time:
-
Permalink:
ipregistry/ipregistry-python@9baaa3a6e2e43c179e3f7328e235bd3d539b9e7b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ipregistry
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@9baaa3a6e2e43c179e3f7328e235bd3d539b9e7b -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file ipregistry-5.0.1-py3-none-any.whl.
File metadata
- Download URL: ipregistry-5.0.1-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97bbc1c1af4d2deeaff5141a62e99c70818126c143293f22c0e4f7e6eb9f2dfe
|
|
| MD5 |
89c864a3bd3995083c0ffd2654fec2b6
|
|
| BLAKE2b-256 |
1a8fe15c2654bf49964b8694bb97091dfd9ebc0943a963e7548de209a0dfce05
|
Provenance
The following attestation bundles were made for ipregistry-5.0.1-py3-none-any.whl:
Publisher:
release.yaml on ipregistry/ipregistry-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ipregistry-5.0.1-py3-none-any.whl -
Subject digest:
97bbc1c1af4d2deeaff5141a62e99c70818126c143293f22c0e4f7e6eb9f2dfe - Sigstore transparency entry: 2125127208
- Sigstore integration time:
-
Permalink:
ipregistry/ipregistry-python@9baaa3a6e2e43c179e3f7328e235bd3d539b9e7b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ipregistry
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@9baaa3a6e2e43c179e3f7328e235bd3d539b9e7b -
Trigger Event:
workflow_dispatch
-
Statement type: