ipregistry/ipregistry-python
GitHub: ipregistry/ipregistry-python
一个官方 Python 客户端库,用于查询 IP 地理位置和威胁情报数据,简化 IP 情报集成。
Stars: 15 | Forks: 2
[
](https://ipregistry.co/)
# Ipregistry Python 客户端库
[](LICENSE)
[](https://github.com/ipregistry/ipregistry-python/actions)
[](https://pypi.org/project/ipregistry/)
这是 [Ipregistry](https://ipregistry.co) IP 地理位置和威胁数据 API 的官方 Python 客户端库,允许您查询您自己的或指定的 IP 地址。响应会返回多个数据点,包括运营商、公司、货币、位置、时区、威胁信息等等。
从库的第 3 版开始,已停止对 Python 2 的支持,该库需要 Python 3.6+。
## 入门指南
您需要一个 Ipregistry API 密钥,您可以通过在 [https://ipregistry.co](https://ipregistry.co) 注册免费账户来获取密钥,并可获得 100,000 次免费查询。
### 安装
```
pip install ipregistry
```
### 快速开始
#### 单个 IP 查询
```
from ipregistry import IpregistryClient
client = IpregistryClient("YOUR_API_KEY")
response = client.lookup_ip("54.85.132.205")
# 打印整个 response
print(response)
# 获取特定 field
country_code = response.data.location.country.code
# 获取 credits 已消耗或剩余的数量
credits_consumed = response.credits.consumed
credits_remaining = response.credits.remaining
```
#### 单个 ASN 查询
```
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)
```
#### 批量 IP 查询
```
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)
```
#### 源 IP 查询
```
from ipregistry import IpregistryClient
client = IpregistryClient("YOUR_API_KEY")
response = client.origin_lookup_ip()
print(response.data)
```
#### User-Agent 解析
```
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)
```
更多高级示例可在 [samples](https://github.com/ipregistry/ipregistry-python/tree/master/samples) 文件夹中找到。
### 缓存
此 Ipregistry 客户端库内置支持内存缓存。默认情况下缓存是禁用的。
以下是启用和配置缓存策略的示例。启用后,默认的缓存策略是最多缓存 2048 次查询,最长保留 10 分钟。您可以按如下方式更改偏好设置:
#### 启用缓存
通过传递一个 `InMemoryCache` 实例来启用缓存:
```
from ipregistry import InMemoryCache, IpregistryClient
client = IpregistryClient("YOUR_API_KEY", cache=InMemoryCache(maxsize=2048, ttl=600))
```
#### 禁用缓存
通过传递一个 `NoCache` 实例来禁用缓存:
```
from ipregistry import IpregistryClient, NoCache
client = IpregistryClient("YOUR_API_KEY", cache=NoCache())
```
### 欧盟基础 URL
使用欧盟基础 URL,您的请求将由欧盟境内最近的节点集群处理:
```
from ipregistry import IpregistryClient, NoCache
client = IpregistryClient(IpregistryConfig("YOUR_API_KEY").with_eu_base_url())
```
### 错误
所有 Ipregistry 异常都继承自 `IpregistryError` 类。
主要子类型为 `ApiError` 和 `ClientError`。
`_ApiError_` 类型的错误包含一个 `code` 字段,该字段映射到 [Ipregistry 文档](https://ipregistry.co/docs/errors)中描述的对应代码。
### 过滤机器人
您可能希望阻止 Ipregistry API 对访问您页面的爬虫或机器人发出请求。
一种可行的方式是使用 `User-Agent` 头部来识别机器人。
为了简化此过程,该库包含一个实用方法:
```
from ipregistry import UserAgents
is_bot = UserAgents.is_bot('YOUR_USER_AGENT_HEADER_VALUE_HERE')
```
## 其他库
有许多语言都有官方的 Ipregistry 客户端库,包括
[Java](https://github.com/ipregistry/ipregistry-java)、[Javascript](https://github.com/ipregistry/ipregistry-javascript) 等等。
您是否在寻找一种我们尚未支持的编程语言或框架的官方客户端?
[请告诉我们](mailto:support@ipregistry.co)。
标签:API客户端, ASN查询, IP地理定位, IP智能, Python, SEO优化, SOC Prime, 云服务, 代码示例, 位置服务, 地理信息, 威胁情报, 客户端库, 开发工具, 开发者工具, 批量查询, 数据分析, 数据查询, 无后门, 系统分析, 编程语言, 网络分析, 网络安全, 资产探测, 逆向工具, 隐私保护