How to verify the Domain Name System (DNS) in Python? (Source Code)


The Domain Name System, or DNS, is a critical component of the internet that helps to map human-friendly domain names to numerical IP addresses. When you type a URL into your web browser, DNS servers are responsible for converting that URL into the corresponding IP address.

Python’s built-in module, dns.resolver, facilitates the resolution of domain names to IP addresses. By default, dns.resolver will use the system’s DNS server, but it can also be configured to use a specific DNS server.

For example, the following code will print out the A record for google.com:

import dns.resolver resolver = dns.resolver.Resolver() resolver.nameservers = ['8.8.8.8'] answer = resolver.query('google.com', 'A') print(answer[0])
Code language: JavaScript (javascript)

This code uses Google’s public DNS server at 8.8.8.8, but you can use any DNS server that you like. To verify that a particular DNS server is working correctly, you can use the dig utility to perform a DNS lookup from the command line

$ dig google.com
<<>> DiG 9 . 11 .3 - 1ubuntu1 . 3 - Debian <<>> @ 8 . 8 . 8 . 8 google . com ; ( 1 server found ) ;; global options : + cmd ;; Got answer : ;; - > HEADER < - opcode : QUERY , status : NOERROR , id : 44987 ;; flags : qr rd ra ; QUERY : 1 , ANSWER : 1 , AUTHORITY : 0 , ADDITIONAL : 0 ;; QUESTION SECTION : ; google . com . IN A ;; ANSWER SECTION : google . com . 299 IN A 172 . 217 . 26 . 36 ;; Query time : 25 msec ;; SERVER : 8 . 8 . 8 . 8 # 53 ( UDP ) ;; WHEN : Mon Nov 21 23:31:14 PST 2017 ;; MSG SIZE rcvd: 55
Code language: CSS (css)

The output of this command shows that the A record for google.com resolves to 172.217.26.36 by using Google’s DNS server at 8.8.8.8 Using dns.resolver, you can easily write Python code to query any DNS server and verify its results.

Full another source code, verifying the Domain Name System (DNS)

# DNS VERIFIER import json import sys from collections import OrderedDict import dns.resolver def checker(dns_val=None) -> OrderedDict: ip_values = None avail = False if dns_val is None: raise ValueError("Sorry DNS not found, DNS is needed") if isinstance(dns_val, str) is False: raise TypeError("Sorry, \'DNS\' must be type \'str\'") try: output = dns.resolver.resolve(dns_val, 'A') ip_values = [ipval.to_text() for ipval in output] except dns.resolver.NXDOMAIN: avail = True return OrderedDict([ ("DNS", dns_val), ("IP", ip_values), ("AVAIL", avail), ]) if __name__ == '__main__': dns_val = None option = None print("Enter the DNS:") dns_val=input() try: response = checker(dns_val=dns_val) except Exception as err: print(f"error: {err}") sys.exit(1) print(json.dumps(response, indent=4)) sys.exit(0)
Code language: Python (python)

This Script to verify the Domain Name System. This is useful to check for the availability of DNS.

Instructions to run this application

  1. Python 3 must be installed in your system.
  • For the first time, run this in terminal or PowerShell
pip3 install -r requirements.txt
Code language: CSS (css)
  1. It will download all the required modules
  • Now run the below command

Example:

python main.py
Code language: CSS (css)

Enter the DNS: abc.com { “DNS”: “abc.com”, “IP”: [ “23.212.223.135” ], “AVAIL”: false }

Download the source code here

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts