Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PHASE 4 - by end of week 6: Exception handling and CLI argument processing By the end of this week the following code sections can

PHASE 4 - by end of week 6: Exception handling and CLI argument processing

By the end of this week the following code sections can be completed:

Write the code to process all command line arguments.

Include exception handling for opening files.

Update the test plan: add a minimum of 7 cases to test CLI argument processing.

Update the test plan: add a minimum of 1 cases to test opening files.

Test your program based on your current test plan version.

Python project: Manage hosts file Section A - Program description Program description

The hosts file management tool is named hostinfo.py.

The hosts file management tool has the following functionality:

oReads hosts file based on search string

Displays all host entries matching the search string

If no search string is provided, displays the entire hosts file

oUpdates hosts file

Validates IPv4 address

Ensures that hostname is not an empty string

Appends host information to hosts file

The script can be executed using command line arguments and interactively.

oWhen executed with command arguments it runs once based on the arguments provided - see below.

oWhen executed interactively the program runs until the user quits.

Program execution options

The hosts file management tool can be run with CLI arguments or interactively.

Implement the following CLI arguments & corresponding program execution:

o./hostsinfo.py -h# displays usage

o./hostsinfo.py -r [search_string] # displays hosts file or hosts matching search string

o./hostsinfo.py -w IP host# appends host info to hosts file

o./hostsinfo.py# executes in interactive mode

For command line processing we will use the sys module.

Program functions

The hosts file management tool includes the following functions.

Create the following functions:

ousage()# displays usage of utility

oshow_menu()# displays menu when in interactive mode

oget_hostinput() # prompts user for host-related data

oisdnsname()# validates hostname

oisip()# validates IP address

oisipoctet()# validates one IP octet

osearch_hosts() # displays matching hosts in host file

oupdate_hosts() # writes host information to hosts file

Function details

ousage()

Function purpose: displays a usage message

Function parameters: none

Function return: none

oshow_menu()

Function purpose: display menu options in interactive mode

Function parameters: none

Function return: none

oget_hostinput()

Function purpose: prompt for input (example: prompt for IP address); used anytime user input is required.

Function parameters: prompt (example: "Enter IP address: ")

Function return: user input (example: IP address)

oisdnsname()

Function purpose: validate hostname

Function parameters: hostname

Function return: True/False

Function body:

Verify that the hostname is not an empty string.

Note: By setting up a function dedicated to hostname validation we can refine our hostname validation at a later date without touching the main program code.

oisipoctet()

Function purpose: validate IP octet

Function parameters: octet

Function return: True/False

Function body:

return True if the octet is a digit in the range between 0 and 255;

return False otherwise

print appropriate error message if a requirement does not apply

oisip()

Function purpose: validate IP address

Function parameters: IP address

Function return: True/False

Function body:

Verify that the IP address has 4 octets.

Note: You may find split() helpful.

Validate each octet by calling the isipoctet function.

If any one octet is invalid the entire IP address invalid.

osearch_hosts()

Function purpose: display matching hosts in host file

Function parameters: optional search string

Note: We will set up the empty string as the default value for the function parameter - the search string - in the function definition header line.

Syntax: def function(parameter="")

Example: def search_hosts(searchstr="")

Function return: list of matching hosts if search string provided or all hosts

Function body:

Open the file in read mode, and return 'None' if an exception occurs.

Match each host in the hosts file with the search string, and return the list of matched hosts.

Note: Alternatively (for better efficiency) return the content of the hosts file if a search string is not provided.

oupdate_hosts()

Function purpose: write host information to hosts file

Function parameters: IP address, hostname

Function return: none

Function body:

open file in append mode

Update hosts file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions