Title: Processing a hosts file with PythonIn this assignment, you will write a Python program which parses a text file that associates IPv4addresses and hostnames, and outputs the requested...

1 answer below »
Title: Processing a hosts file with PythonIn this assignment, you will write a Python program which parses a text file that associates IPv4addresses and hostnames, and outputs the requested information. Purely for reference, the file issimilar in format to the actual Unix file /etc/hosts.These are the specifications for your Python program:1. It must be named hosts.py2. It should be invoked as:python hosts.py option hosts_fileThe program must check that argument hosts_file exists, is a file and is readable. If not, itmust print an error message to the standard output and exit. The specifications for thehosts_file and option arguments are given below.3. File hosts_file can have any arbitrary name. It must be a file of text with the following format:a. The file consists of an arbitrary number of lines (including, possibly, zero lines).b. Each line must contain three fields separated by one or more space characters.c. The three fields are: IPv4 address, hostname, alias.d. The IPv4 address field is a string of characters in the well-known IPv4 address dotdecimalformat (four integers, each between 0 and 255, separated by dots; example:103.246.98.253).e. The hostname field is a string of characters that can include letters, digits, hyphens(also known as minus signs) and dots. Its length can vary from a minimum of 4 to amaximum of 253 characters.f. The alias field is a string of the same type of characters, varying in length from aminimum of 1 character to an unlimited (yet sensible) number.Fundamental note: your program is not expected to verify that file hosts_file complies withthe above specifications. It will only be tested with compliant files.The following example should be regarded as the reference specification for the format of filehosts_file:127.0.0.1 localhost.localdomain sahara192.168.1.10 foo.mydomain.edu foo209.237.226.90 www.opensource.org picco192.168.1.13 bar.mydomain.com bar146.82.138.7 master.debian.org master44. Your program can be invoked with option: -a. In this case, it must print all the hostnames inthe order in which they appear in file hosts_file, in this format:Hostnames:…Example with file hosts_file given above:Command line:python hosts.py -a hosts_fileOutput:Hostnames:localhost.localdomainfoo.mydomain.eduwww.opensource.orgbar.mydomain.commaster.debian.orgIn the case in which file hosts_file is empty, your program must instead only print:No hosts5. Your program can be invoked with option: -d domain. The domain argument is a string thatrepresents the top-level domain of a hostname (the string after the right-most dot in thehostname). For instance, in the file hosts_file given above, strings localdomain, edu, organd com are all top-level domains. Note that there is no dot in the top-level domain.In this case, your program must print all the lines of file hosts_file where the top-level domainof the hostname matches the given domain argument, in the order in which they appear inthe file. This is an example with file hosts_file given above:Command line:python hosts.py -d org hosts_fileOutput:209.237.226.90 www.opensource.org picco146.82.138.7 master.debian.org masterIn the case in which no line of file hosts_file matches the domain argument (including thecase where the file is empty), your program must print:5No hosts in the given domain6. Your program can be invoked with option: -c class. The class argument is a string of onecharacter that can only take values A, B or C (please note: only uppercase).(Note: purely for reference, this option is inspired by a superseded feature of IP addressesknown as classful network design.)In the case in which the value is A, your program must print all the lines of file hosts_filewhere the number before the first dot in the IPv4 address field is between 0 and 127,included, in the order in which they appear in the file. This is an example with file hosts_filegiven above:Command line:python hosts.py -c A hosts_fileOutput:127.0.0.1 localhost.localdomain saharaIn the case in which the value is B, your program must print all the lines of file hosts_filewhere the number before the first dot in the IPv4 address field is between 128 and 191,included, in the order in which they appear in the file. This is an example with file hosts_filegiven above:Command line:python hosts.py -c B hosts_fileOutput:146.82.138.7 master.debian.org masterIn the case in which the value is C, your program must print all the lines of file hosts_filewhere the number before the first dot in the IPv4 address field is between 192 and 255,included, in the order in which they appear in the file. This is an example with file hosts_filegiven above:Command line:python hosts.py -c C hosts_fileOutput:192.168.1.10 foo.mydomain.edu foo209.237.226.90 www.opensource.org picco192.168.1.13 bar.mydomain.com barIn the case in which no line of file hosts_file matches the class argument (including the casewhere the file is empty), your program must print:6No hosts in the given class7. Your program can be invoked with option: -v. In this case, it must only print your name,surname, student ID and date of completion of your assignment, in a format of your choice.Please note that argument hosts_file is still required.8. No options can be used simultaneously. This means that your program can only be invokedwith one of the options at a time.9. If your program is invoked with any other syntax than those specified above, it must print amessage of your choice to the standard output and exit.Examples of incorrect syntax:python hosts.py -Z hosts_filepython hosts.py -apython hosts.py -c DPlease be reminded that: This assignment must be your own work and you should not be helped by anyone to prepareit in any way; your assignment may be tested by anti-plagiarism software that detectssuperficial changes such as changing variable names, swapping lines of code and the like. Understanding the assignment specifications is part of the assignment itself and no furtherinstructions will be provided; on the other hand, whatever is not constrained you canimplement it according to your own best judgment
Answered Same DayOct 20, 2021

Answer To: Title: Processing a hosts file with PythonIn this assignment, you will write a Python program which...

Kushal answered on Oct 23 2021
144 Votes
127.0.0.1 localhost.localdomain sahara
192.168.1.10 foo.mydomain.edu foo
209.
237.226.90 www.opens.org picco
192.168.1.13 bar.mydomain.com bar
146.82.138.7 master.debian.org master
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here