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 and the library requires Python 3.6+.
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)
More advanced examples are available in the samples folder.
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
With the ever-increasing concerns about data privacy and security, we recognize the importance of providing our European users with a dedicated API base URL:
from ipregistry import IpregistryClient, NoCache
client = IpregistryClient(IpregistryConfig("YOUR_API_KEY").with_eu_base_url())
Using this base URL your requests are handled by the closest cluster of nodes in the European Union.
Errors
All Ipregistry exceptions inherit IpregistryError
class.
Main subtypes are ApiError
and ClientError
.
Errors of type ApiError include a code field that maps to the one described in the Ipregistry documentation.
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
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
File details
Details for the file ipregistry-4.0.0.tar.gz
.
File metadata
- Download URL: ipregistry-4.0.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.3 Linux/6.8.11-300.fc40.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7041e36657e24cc57d004a7ef76fd60859b9760f9d5a16a912f9f3ec1d2e45a |
|
MD5 | b6a1a5fcec2949a5c6761db2cb79ef4b |
|
BLAKE2b-256 | 3e11458b5ca7cbda087130f278ae34bac5092fbd476f0769684d82da1da946db |
File details
Details for the file ipregistry-4.0.0-py3-none-any.whl
.
File metadata
- Download URL: ipregistry-4.0.0-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.3 Linux/6.8.11-300.fc40.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2013b7c287e490e035c62aebca597cc0084984302cf2adbbeb7d9d11be474c6 |
|
MD5 | 7a2ce3605abb934103111241d02c3401 |
|
BLAKE2b-256 | b91ed0fe6e377072126c6ba79294bb98f2d562c9066cb9b4246bc76fe0a190a3 |