简体   繁体   English

无法使用 cloudflare 和 python 请求创建 dns-over-https

[英]Unable to make dns-over-https with cloudflare and python requests

I'm trying to write a quick script that could do dns lookups using the new 1.1.1.1 DNS over HTTPS public DNS server from CloudFlare.我正在尝试编写一个快速脚本,该脚本可以使用 CloudFlare 的 HTTPS 公共 DNS 服务器上的新 1.1.1.1 DNS 进行 dns 查找。

Looking at their docs here https://developers.cloudflare.com/1.1.1.1/dns-over-https/json-format/ I'm not sure what I'm doing wrong and why I'm getting a 415 status code (415 Unsupported content type).在此处查看他们的文档https://developers.cloudflare.com/1.1.1.1/dns-over-https/json-format/我不确定我做错了什么以及为什么我收到 415 状态代码(415 不支持的内容类型)。

Here is my script: #!/usr/bin/env python import requests import json from pprint import pprint这是我的脚本:#!/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()

Here is the output:这是输出:

    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 415 Client Error: Unsupported Media Type for url: https://cloudflare-dns.com/dns-query

and for the json response (expected I believe):对于 json 响应(预计我相信):

raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Using curl this works perfectly fine.使用 curl 可以很好地工作。

Many thanks非常感谢

You should not set a JSON request at all .你不应该设置在所有的JSON请求。 The response uses JSON.响应使用 JSON。

Put the application/dns-json value in a ct parameter:application/dns-json值放在ct参数中:

JSON formatted queries are sent using a GET request.使用 GET 请求发送 JSON 格式的查询。 When making requests using GET, the DNS query is encoded into the URL.使用 GET 发出请求时,DNS 查询被编码到 URL 中。 An additional URL parameter of 'ct' should indicate the MIME type (application/dns-json). 'ct' 的附加 URL 参数应指示 MIME 类型 (application/dns-json)。

A GET request never has a body, so don't try to send JSON: GET 请求从来没有正文,所以不要尝试发送 JSON:

params = {
    'name': 'example.com',
    'type': 'A',
    'ct': 'application/dns-json',
}
ae = client.get(url, params=params)

Demo:演示:

>>> import requests
>>> url = 'https://cloudflare-dns.com/dns-query'
>>> client = requests.session()
>>> params = {
...     'name': 'example.com',
...     'type': 'A',
...     'ct': 'application/dns-json',
... }
>>> ae = client.get(url, params=params)
>>> ae.status_code
200
>>> from pprint import pprint
>>> pprint(ae.json())
{'AD': True,
 'Answer': [{'TTL': 2560,
             'data': '93.184.216.34',
             'name': 'example.com.',
             'type': 1}],
 'CD': False,
 'Question': [{'name': 'example.com.', 'type': 1}],
 'RA': True,
 'RD': True,
 'Status': 0,
 'TC': False}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM