Open In App

Network Programming in Python – DNS Look-up

Last Updated : 05 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Domain Name System also known as DNS is a phonebook of the internet, which has related to the domain name. DNS translates the domain names to the respective IP address so that browsers can access the resources. Python provides DNS module which is used to handle this translation of domain names to IP addresses.

Finding Records

The dnspython module provides dns.resolver() helps to find out various records of a domain name. The function takes two important parameters, the domain name, and the record type. Some of the record types with examples are listed below :

  • A Record: It is fundamental type of DNS record, here A stands for address. It shows the IP address of the domain.

Output:

A Record :  34.218.62.116
  • AAAA Record: This is an IP address record, used to find the IP of the computer connected to the domain. It is conceptually similar to A record but specifies only the IPv6 address of the server rather than IPv4.
Python3
# Import libraries
import dns.resolver

# Finding AAAA record
result = dns.resolver.resolve('geeksforgeeks.org', 'AAAA')

# Printing record
for val in result:
    print('AAAA Record : ', ipval.to_text())

Output:

NoAnswer: The DNS response does not contain an answer to the question: geeksforgeeks.org. IN AAAA

  • PTR Record: PTR stands for pointer record, used to translate IP addresses to the domain name or hostname. It is used to reverse the DNS lookup.
Python3
# Import libraries
import dns.resolver

# Finding PTR record
result = dns.resolver.resolve('116.62.218.34.in-addr.arpa', 'PTR')

# Printing record
for val in result:
    print('PTR Record : ', val.to_text())

Output:

PTR Record :  ec2-34-218-62-116.us-west-2.compute.amazonaws.com.

  • NS Record: Nameserver(NS) record gives information that which server is authoritative for the given domain i.e. which server has the actual DNS records. Multiple NS records are possible for a domain including the primary and the backup name servers.
Python3
# Import libraries
import dns.resolver

# Finding NS record
result = dns.resolver.resolve('geeksforgeeks.org', 'NS')

# Printing record
for val in result:
    print('NS Record : ', val.to_text())

Output:

NS Record :  ns-1520.awsdns-62.org.
NS Record : ns-1569.awsdns-04.co.uk.
NS Record : ns-245.awsdns-30.com.
NS Record : ns-869.awsdns-44.net.
  • MX Records: MX stands for Mail Exchanger record, which is a resource record that specifies the mail server which is responsible for accepting emails on behalf of the domain. It has preference values according to the prioritizing mail if multiple mail servers are present for load balancing and redundancy.
Python3
# Import libraries
import dns.resolver

# Finding MX record
result = dns.resolver.resolve('geeksforgeeks.org', 'MX')

# Printing record
for val in result:
    print('MX Record : ', val.to_text())

Output:

MX Record :  1 aspmx.l.google.com.
MX Record : 10 alt3.aspmx.l.google.com.
MX Record : 10 alt4.aspmx.l.google.com.
MX Record : 5 alt1.aspmx.l.google.com.
MX Record : 5 alt2.aspmx.l.google.com.
  • SOA Records: SOA stands for Start of Authority records, which is a type of resource record that contains information regarding the administration of the zone especially related to zone transfers defined by the zone administrator.
Python3
# Import libraries
import dns.resolver

# Finding SOA record
result = dns.resolver.resolve('geeksforgeeks.org', 'SOA')

# Printing record
for val in result:
    print('SOA Record : ', val.to_text())

Output:

SOA Record :  ns-869.awsdns-44.net. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400

  • CNAME Record: CNAME stands for Canonical  Name record, which is used in mapping the domain name as an alias for the other domain. It always points to another domain and never directly points to an IP.
Python3
# Import libraries
import dns.resolver

# Finding CNAME record
result = dns.resolver.resolve('geeksforgeeks.org', 'CNAME')

# Printing record
for val in result:
    print('CNAME Record : ', val.target)

Output:

NoAnswer: The DNS response does not contain an answer to the question: geeksforgeeks.org. IN CNAME

  • TXT Record: These records contain the text information of the sources which are outside of the domain. TXT records can be used for various purposes like google use them to verify the domain ownership and to ensure email security.
Python3
# Import libraries
import dns.resolver

# Finding TXT record
result = dns.resolver.resolve('geeksforgeeks.org', 'TXT')

# Printing record
for val in result:
    print('TXT Record : ', val.to_text())

Output:

TXT Record :  “fob1m1abcdp777bf2ncvnjm08n” TXT Record :  “v=spf1 include:amazonses.com include:_spf.google.com ip4:167.89.66.115 -all”



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads