How to do it...

We import dns.name of dnspython to do a simple exercise to find the DNS names from the user inputs of two web URLs, and how these web URLs are related.

Listing 11.1 evaluates the user input of two web URLs for the DNS names 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.name def main(site1, site2): _site1 = dns.name.from_text(site1) _site2 = dns.name.from_text(site2) print("site1 is subdomain of site2: ",
_site1.is_subdomain(_site2)) print("site1 is superdomain of site2: ",
_site1.is_superdomain(_site2)) print("site1 labels: ", _site1.labels) print("site2 labels: ", _site2.labels) if __name__ == '__main__': parser = argparse.ArgumentParser(description=
'DNS Python') parser.add_argument('--site1', action="store",
dest="site1", default='www.dnspython.org') parser.add_argument('--site2', action="store",
dest="site2", default='dnspython.org') given_args = parser.parse_args() site1 = given_args.site1 site2 = given_args.site2 main (site1, site2)

The code performs a check to see whether the site1 is either a subdomain or a superdomain of site2, and it finally prints the labels of the sites.

Executing this with two arguments produces the following output:

$ python 11_1_dns_names_with_dnspython.py --site1="edition.cnn.com" --site2="cnn.com"
('site1 is subdomain of site2: ', True)
('site1 is superdomain of site2: ', False)
('site1 labels: ', ('edition', 'cnn', 'com', ''))
('site2 labels: ', ('cnn', 'com', ''))
    
$ python 11_1_dns_names_with_dnspython.py --site1="edition.cnn.com" --site2="www.cnn.com"
('site1 is subdomain of site2: ', False)
('site1 is superdomain of site2: ', False)
('site1 labels: ', ('edition', 'cnn', 'com', ''))
('site2 labels: ', ('www', 'cnn', 'com', ''))
    
$ python 11_1_dns_names_with_dnspython.py --site1="edition.cnn.com" --site2="edition.cnn.com"
('site1 is subdomain of site2: ', True)
('site1 is superdomain of site2: ', True)
('site1 labels: ', ('edition', 'cnn', 'com', ''))
('site2 labels: ', ('edition', 'cnn', 'com', ''))
..................Content has been hidden....................

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