Network Programming in Python - DNS Look-up - GeeksforGeeks

25 Oct, 2020
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.
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.
Python3
# Import librariesimport dns.resolver  # Finding A recordresult = dns.resolver.query('geeksforgeeks.org', 'A')  # Printing recordfor val in result:    print('A Record : ', val.to_text())
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 librariesimport dns.resolver  # Finding AAAA recordresult = dns.resolver.query('geeksforgeeks.org', 'AAAA')  # Printing recordfor 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 librariesimport dns.resolver  # Finding PTR recordresult = dns.resolver.query('116.62.218.34.in-addr.arpa', 'PTR')  # Printing recordfor 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 librariesimport dns.resolver  # Finding NS recordresult = dns.resolver.query('geeksforgeeks.org', 'NS')  # Printing recordfor 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 librariesimport dns.resolver  # Finding MX recordresult = dns.resolver.query('geeksforgeeks.org', 'MX')  # Printing recordfor 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 librariesimport dns.resolver  # Finding SOA recordresult = dns.resolver.query('geeksforgeeks.org', 'SOA')  # Printing recordfor 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 librariesimport dns.resolver  # Finding CNAME recordresult = dns.resolver.query('geeksforgeeks.org', 'CNAME')  # Printing recordfor 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 librariesimport dns.resolver  # Finding TXT recordresult = dns.resolver.query('geeksforgeeks.org', 'TXT')  # Printing recordfor 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”
Python-Networking
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Python | os.path.join() method
Defaultdict in Python
Selecting rows in pandas DataFrame based on conditions
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()