lost and found ( for me ? )

How to send DNS queries over HTTPS to Google Public DNS with Python

Here is a sample python script of how to send DNS queries over HTTPS to Google Public DNS.

Reference
https://developers.google.com/speed/public-dns/docs/dns-over-https#supported_parameters

This script sends 3 DNS queries over HTTPS in a row with single TCP connection.
hattori@ubuntu:~/Python_works$ cat dns_over_https_keepalive.py
import requests

s = requests.Session()
res = s.get('https://dns.google.com/resolve?name=www.google.com&edns_client_subnet=100.0.0.0/24')
res_dict = res.json()
#for key, value in res_dict.iteritems():
#    print key, value
print "All responses\n", res_dict, "\n"
print "Question\n", res_dict['Question'], "\n"
print "Answer\n", res_dict['Answer'], "\n"

print "---\n"

res = s.get('https://dns.google.com/resolve?name=www.google.com&edns_client_subnet=30.0.0.0/24')
res_dict = res.json()
print "All responses\n", res_dict, "\n"
print "Question\n", res_dict['Question'], "\n"
print "Answer\n", res_dict['Answer'], "\n"

print "---\n"

res = s.get('https://dns.google.com/resolve?name=www.apple.com&edns_client_subnet=50.0.0.0/24')
res_dict = res.json()
print "All responses\n", res_dict, "\n"
print "Question\n", res_dict['Question'], "\n"
print "Answer\n", res_dict['Answer'], "\n"

Sample output
hattori@ubuntu:~/Python_works$ python dns_over_https_keepalive.py
All responses
{u'Status': 0, u'Comment': u'Response from 216.239.32.10', u'edns_client_subnet': u'100.0.0.0/21', u'AD': False, u'Question': [{u'type': 1, u'name': u'www.google.com.'}], u'CD': False, u'RD': True, u'RA': True, u'Answer': [{u'data': u'172.217.4.196', u'type': 1, u'name': u'www.google.com.', u'TTL': 299}], u'TC': False, u'Additional': []}

Question
[{u'type': 1, u'name': u'www.google.com.'}]

Answer
[{u'data': u'172.217.4.196', u'type': 1, u'name': u'www.google.com.', u'TTL': 299}]

---

All responses
{u'Status': 0, u'Comment': u'Response from 216.239.32.10', u'edns_client_subnet': u'30.0.0.0/32', u'AD': False, u'Question': [{u'type': 1, u'name': u'www.google.com.'}], u'CD': False, u'RD': True, u'RA': True, u'Answer': [{u'data': u'216.58.199.100', u'type': 1, u'name': u'www.google.com.', u'TTL': 299}], u'TC': False, u'Additional': []}

Question
[{u'type': 1, u'name': u'www.google.com.'}]

Answer
[{u'data': u'216.58.199.100', u'type': 1, u'name': u'www.google.com.', u'TTL': 299}]

---

All responses
{u'Status': 0, u'Comment': u'Response from 96.7.248.135', u'edns_client_subnet': u'50.0.0.0/0', u'AD': False, u'Question': [{u'type': 1, u'name': u'www.apple.com.'}], u'CD': False, u'RD': True, u'RA': True, u'Answer': [{u'data': u'www.apple.com.edgekey.net.', u'type': 5, u'name': u'www.apple.com.', u'TTL': 1145}, {u'data': u'www.apple.com.edgekey.net.globalredir.akadns.net.', u'type': 5, u'name': u'www.apple.com.edgekey.net.', u'TTL': 21599}, {u'data': u'e6858.dscc.akamaiedge.net.', u'type': 5, u'name': u'www.apple.com.edgekey.net.globalredir.akadns.net.', u'TTL': 3599}, {u'data': u'104.113.107.126', u'type': 1, u'name': u'e6858.dscc.akamaiedge.net.', u'TTL': 19}], u'TC': False, u'Additional': []}

Question
[{u'type': 1, u'name': u'www.apple.com.'}]

Answer
[{u'data': u'www.apple.com.edgekey.net.', u'type': 5, u'name': u'www.apple.com.', u'TTL': 1145}, {u'data': u'www.apple.com.edgekey.net.globalredir.akadns.net.', u'type': 5, u'name': u'www.apple.com.edgekey.net.', u'TTL': 21599}, {u'data': u'e6858.dscc.akamaiedge.net.', u'type': 5, u'name': u'www.apple.com.edgekey.net.globalredir.akadns.net.', u'TTL': 3599}, {u'data': u'104.113.107.126', u'type': 1, u'name': u'e6858.dscc.akamaiedge.net.', u'TTL': 19}]

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.