How to do it...

We import dns.reversename of dnspython to do a simple exercise of finding the reverse name of an address from the given address. We use dns.resolver to find the address that an IP address resolves to be.

Listing 11.2 gives the domain information for a given IP address as follows:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition 
-- Chapter - 11 # This program is optimized for Python 2.7.12 and
Python 3.5.2. # It may run on any other version with/without
modifications. import argparse import dns.reversename import dns.resolver def main(address): n = dns.reversename.from_address(address) print(n) print(dns.reversename.to_address(n)) try: # Pointer records (PTR) maps a network
interface (IP) to the host name. domain = str(dns.resolver.query(n,"PTR")[0]) print(domain) except Exception as e: print ("Error while resolving %s: %s" %(address, e)) if __name__ == '__main__': parser = argparse.ArgumentParser(description='DNS Python') parser.add_argument('--address', action="store",
dest="address", default='127.0.0.1') given_args = parser.parse_args() address = given_args.address main (address)

We run this recipe with various inputs for addresses. First with localhost, followed by two valid addresses in the internet, and then an invalid address.

$ python 11_2_dns_host_with_dnspython.py --address="127.0.0.1"
1.0.0.127.in-addr.arpa.
b'127.0.0.1'
localhost.
    
$ python 11_2_dns_host_with_dnspython.py --address="216.58.199.78"
78.199.58.216.in-addr.arpa.
b'216.58.199.78'
syd15s01-in-f78.1e100.net.
    
$ python 11_2_dns_host_with_dnspython.py --address="172.217.19.193"
193.19.217.172.in-addr.arpa.
b'172.217.19.193'
ams16s31-in-f1.1e100.net.
    
$ python 11_2_dns_host_with_dnspython.py --address="52.95.3.61"
61.3.95.52.in-addr.arpa.
b'52.95.3.61'
Error while resolving 52.95.3.61: The DNS query name does not exist: 61.3.95.52.in-addr.arpa.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.103.154