mr4*_*ino 0 dns json python-requests dns-over-https
我正在尝试编写一个快速脚本,可以使用来自 CloudFlare 的新 1.1.1.1 DNS over HTTPS 公共 DNS 服务器进行 dns 查找。
在这里查看他们的文档https://developers.cloudflare.com/1.1.1.1/dns-over-https/json-format/我不确定我做错了什么以及为什么我收到 415 状态代码(415 不支持的内容类型)。
这是我的脚本: #!/usr/bin/env python import requests import json from pprint import pprint
url = 'https://cloudflare-dns.com/dns-query'
client = requests.session()
json1 = {'name': 'example.com','type': 'A'}
ae = client.get(url, headers = {'Content-Type':'application/dns-json'}, json = json1)
print ae.raise_for_status()
print ae.status_code
print ae.json()
client.close()
Run Code Online (Sandbox Code Playgroud)
这是输出:
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 415 Client Error: Unsupported Media Type for url: https://cloudflare-dns.com/dns-query
Run Code Online (Sandbox Code Playgroud)
对于 json 响应(我相信):
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Run Code Online (Sandbox Code Playgroud)
使用curl 效果非常好。
非常感谢
您根本不应该设置 JSON 请求。响应使用JSON。
\n\n将application/dns-json值放入参数中ct:
\n\n\nJSON 格式的查询是使用 GET 请求发送的。使用 GET 发出请求时,DNS 查询会编码到 URL 中。\xe2\x80\x98ct\xe2\x80\x99 的附加 URL 参数应指示 MIME 类型 (application/dns-json)。
\n
GET 请求永远不会有正文,因此不要尝试发送 JSON:
\n\nparams = {\n 'name': 'example.com',\n 'type': 'A',\n 'ct': 'application/dns-json',\n}\nae = client.get(url, params=params)\nRun Code Online (Sandbox Code Playgroud)\n\n演示:
\n\n>>> import requests\n>>> url = 'https://cloudflare-dns.com/dns-query'\n>>> client = requests.session()\n>>> params = {\n... 'name': 'example.com',\n... 'type': 'A',\n... 'ct': 'application/dns-json',\n... }\n>>> ae = client.get(url, params=params)\n>>> ae.status_code\n200\n>>> from pprint import pprint\n>>> pprint(ae.json())\n{'AD': True,\n 'Answer': [{'TTL': 2560,\n 'data': '93.184.216.34',\n 'name': 'example.com.',\n 'type': 1}],\n 'CD': False,\n 'Question': [{'name': 'example.com.', 'type': 1}],\n 'RA': True,\n 'RD': True,\n 'Status': 0,\n 'TC': False}\nRun Code Online (Sandbox Code Playgroud)\n