
Hi! Let’s use Python to do a Reverse DNS Lookup! We will be using Python 3.8.10. Let’s go! ⚡✨
With a reverse DNS lookup, you already have the IP address and you want to find the associated domain name. It is the opposite of the usual DNS Lookup or Query where you have the domain name and wish to find the IP. Let’s write some code:
import dns.resolver, dns.reversename
addrs = dns.reversename.from_address("8.8.8.8")
print(str(dns.resolver.resolve(addrs,"PTR")[0]))
#output
#dns.google.
- First we import the dnspython library. If you don’t have it installed you can use the pip install dnspython command to install it. The dnspython library gives us powerful tools with which to achieve DNS operations, including our reverse DNS lookup.
- We import two methods that we will use: dns.resolver and dns.reversename.
- The dns.reversename.from_address method converts an IPv4 or IPv6 address into a name object of class dns.name.Name. We must supply the string literal IP address that we want to lookup as the parameter text. In this case we are using “8.8.8.8”. It returns a name object which we store as addrs.
- The dns.resolver.resolve method will actually query the nameserver of the domain for the domain name. In this case, we supply two parameters: qname and rdtype. The qname in this case is the name object addrs that we retrieved in the previous line. The rdtype is the record type we want which is the “PTR” record on this occasion. This method will return a dns.resolver.Answer list object that will contain the domain name we seek. We use the index 0 for this example because we want to retrieve the first element of this dns.resolver.Answer object.
Easy right? When the code executes we should get a proper domain name printed to the screen.
The ‘PTR’ or DNS Pointer record exists for security purposes. The mail server uses the ‘PTR’ record to check that the mail server that’s sending the mail matches its proper IP address as opposed to being from a fraudulent domain. The ‘PTR’ record has the genuine domain name that matches our IP address.
Thanks for reading our Python Reverse DNS Lookup tutorial. Find the complete dnspython documentation HERE.
Also find a tutorial on doing a classic DNS lookup (using a domain or machine name) HERE.

Are you worried about your child’s online safety or your employees’ productivity? Do you wonder what they’re accessing on their devices? SentryPC is here to address these concerns. This all-in-one, cloud-based software provides robust activity monitoring, content filtering, and time management, making it ideal for both parental control and employee monitoring. Embrace peace of mind and enhanced efficiency with SentryPC, the proactive solution to your digital monitoring needs. CLICK HERE to get started.