使用 Python 获取无污染的 DNS 解析结果

 Programing  comment

使用 Python 获取无污染的 DNS 解析结果,解析源使用 CloudFlare 。

示例源码

#!/usr/bin/env python3

# 引入模块
import requests
# 原始地址
url = "https://cloudflare-dns.com/dns-query"
# 指定 Header
headers = {'accept': 'application/dns-json'}
# 可变参数
request_name = "google.com"
request_type = "A"
# 请求参数
payload = {'name': request_name, 'type': request_type}
# 获取
r = requests.get(url=url, headers=headers, params=payload)
# 返回检查
if r.status_code == requests.codes.ok:
    data = r.json()
    answer_name = data["Answer"][0]["name"]
    answer_type = data["Answer"][0]["type"]
    answer_ttl = data["Answer"][0]["TTL"]
    answer_data = data["Answer"][0]["data"]
else:
    r.raise_for_status()

# print(data)
print(answer_name)
print(answer_type)
print(answer_ttl)
print(answer_data)

测试

/Users/kane/PycharmProjects/PureDNS/venv/bin/python /Users/kane/PycharmProjects/PureDNS/PureDNS.py
google.com.
1
299
172.217.6.78

Process finished with exit code 0

参考链接

回复