-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorials
-
Python2 >=
2.7.9
或 Python3 >=3.3.0
pip install --upgrade dnsdb-python-sdk
在Mac OS X使用上面的方法安装, 在使用时可能出现以下错误:
requests.exceptions.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:590)
这是Mac OS X pyOpenSSL版本太旧导致, 您可以通过homebrew重新安装python解决,具体安装步骤如下:
-
通过brew安装python
brew install python --with-brewed-openssl
-
将python的bin路径加入到PATH环境变量中
export PATH=/usr/local/Cellar/python/2.7.11/bin/:$PATH
-
创建符号链接
mkdir -p /usr/local/opt/python/bin/ ln -s /usr/local/Cellar/python/2.7.11/bin/python2.7 /usr/local/opt/python/bin/python2.7
-
安装
pip install --upgrade dnsdb-python-sdk
DnsDB Python API主要使用dnsdb_sdk.api.APIClient
类。
APIClient
常用的几个方法:
-
get_api_user
获取当前API User的信息。 -
search_dns
查询DNS记录,支持分页查询,最多可获取前10000条查询结果。 -
scan_dns
查询DNS记录,不支持分页查询,可以获取所有的查询结果。
APIClient
在初始化时需要指定api_id
和api_key
from __future__ import print_function
from dnsdb_sdk.api import APIClient
api_id = "your API ID"
api_key = "your API key"
client = APIClient(api_id, api_key)
如果您还没有
api_key
和api_id
, 你可以查看这里
通过APIClient.get_api_user
获取当前API User的信息。
api_user = client.get_api_user()
print(api_user.remaining_requests) # 打印该API User的剩余API请求次数
print(api_user.user) # 打印该API User所属的用户名
查询DNS记录可以使用APIClient.search_dns
方法, 该方法单次调用最多返回50条查询结果, 每成功调用一次扣除一次API查询次数,同一个查询条件最多获取前10000条结果。
APIClient.search_dns
至少需要指定domain
,host
,ip
,value_domain
,value_host
,value_ip
,
查询某个顶级私有域名的DNS记录
result = client.search_dns(domain='github.com')
查询指定host值的DNS记录
result = client.search_dns(host='www.github.com')
查询解析到8.8.8.8的DNS记录
result = client.search_dns(ip='8.8.8.8')
查询某个顶级私有域名的所有A记录
result = client.search_dns(domain='github.com', dns_type='A)
指定page
参数, 可以获取指定页码的查询结果, page
默认为1
result = client.search_dns(domain='github.com', page=2) # 获取第二页查询结果
result
是dnsdb_sdk.api.SearchResult
的实例
print(result.total) # 查询结果总数
print(result.remaining_request) # 剩余查询次数
print(len(result)) # 当前返回的结果数量
遍历查询结果
for record in result:
print(record.host,record.type, record.value)
如果您想获取某个DNS查询的所有结果,您可以使用APIClient.scan_dns
方法实现, 调用该方法会扣除一次API请求次数。遍历结果时会根据遍历的数据量扣除相应的API请求次数。
查询某个顶级域名的所有DNS记录
result = client.scan_dns(domain='github.com') # 这里会扣除一次API请求次数
result
是dnsdb_sdk.api.ScanResult
的实例。获取满足查询条件的结果总数
total = len(result) # 这里不会扣除API请求次数
遍历查询结果, 这里会根据你遍历的查询结果数扣除相应的API请求次数, 如果您提前结束遍历, 可能会减少扣除的API请求次数
for record in result:
print(record)
遍历
ScanResult
时, 它会自动多次调用APIClient.next_dns_scan
,每调用一次,扣除一次API请求次数。
from dnsdb_sdk.api import APIClient
api_id = "your API ID"
api_key = "your API key"
proxies = {
'http': 'http://10.10.1.10:3128',
'https': 'http://10.10.1.10:1080',
}
client = APIClient(api_id, api_key, proxies=proxies)
代理服务器使用HTTP Basic Auth时,格式为:http://user:password@host/
:
proxies = {'http': 'http://user:pass@10.10.1.10:3128/'}
from dnsdb_sdk.api import APIClient
api_id = "your API ID"
api_key = "your API key"
proxies = {
'http': 'socks5://user:pass@host:port',
'https': 'socks5://user:pass@host:port'
}
client = APIClient(api_id, api_key, proxies=proxies)