Back to articles
DNS Over HTTPS Is Free — Here's How to Use It as an API (Python)

DNS Over HTTPS Is Free — Here's How to Use It as an API (Python)

via Dev.to TutorialAlex Spinov

Most DNS tools are command-line only ( dig , nslookup ). But Cloudflare and Google offer DNS over HTTPS — you can query DNS from Python like any other API. Cloudflare DNS API (No Key) import requests def dns_lookup ( domain , record_type = ' A ' ): resp = requests . get ( ' https://cloudflare-dns.com/dns-query ' , params = { ' name ' : domain , ' type ' : record_type }, headers = { ' Accept ' : ' application/dns-json ' }) return resp . json (). get ( ' Answer ' , []) # A records for r in dns_lookup ( ' google.com ' , ' A ' ): print ( f " { r [ ' name ' ] } → { r [ ' data ' ] } (TTL: { r [ ' TTL ' ] } s) " ) All Record Types # MX records (email servers) for r in dns_lookup ( ' google.com ' , ' MX ' ): print ( f " Mail: { r [ ' data ' ] } " ) # TXT records (SPF, DKIM, etc) for r in dns_lookup ( ' google.com ' , ' TXT ' ): print ( f " TXT: { r [ ' data ' ][ : 80 ] } " ) # NS records (nameservers) for r in dns_lookup ( ' google.com ' , ' NS ' ): print ( f " NS: { r [ ' data ' ] } " ) # CNA

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
8 views

Related Articles