This post will describe the exercises and solutions for week four of Kirk Byers Python for Network Engineers.

The first exercise is the following:

I. Prompt a user to input an IP address.  

Re-using some of the code from class3, exercise4--determine if the IP address is valid. 

Continue prompting the user to re-input an IP address until a valid IP address is input.

Compared to our last script we want to keep asking the user for an IP address until they supply a valid one. This means that we need a loop that can run until some condition changes. This is where While loops come in handy. We will create a Boolean variable called not_done and set this to True.

not_done = True

while not_done:

The meaning of while not_done: is that the While loop will run as long as not_done is True.

The next step is to ask the user for an IP address. We use the built-in function input() to do this.

ip_add = input("\n\nPlease enter an IP address: ")

We use another Boolean variable called valid_ip which is set to True until we prove that the IP address is not valid.

valid_ip = True

We will split the IP address into four octets using the built-in function split() and use the dot as a delimiter. We use an If statement to check that we have four octets, if not the loop will continue meaning that it goes back up to the top instead of moving further down in our code.

octets = ip_add.split(".")
	if len(octets) != 4:
		print("\nSorry but you didn't put in four octets. Please try again.")
		continue

The next step after that is to convert our strings in the list octets to integers. We use the try/except code from last time and raise a ValueError if we don’t succeed in the conversion and set valid_ip to False. The built-in enumerate() function is used to get an index.

for index, octet in enumerate(octets):
		try:
			octets[index] = int(octet)
		except ValueError:
			valid_ip = False

If the IP address is not valid, we use continue once again to print a text and go back to the top of the While loop. The if not valid_ip: part checks that valid_ip isn’t True, meaning that it’s False.

if not valid_ip:
		print("""\nYou entered an octet that wasn't an integer or a blank -
			that's not going to work""")
		continue

We then reuse the same code that we had in the last script to check if the IP address is valid.

first_octet, second_octet, third_octet, fourth_octet = octets

	if first_octet < 1:
		valid_ip = False
	elif first_octet > 223:
		valid_ip = False
	elif first_octet == 127:
		valid_ip = False

	if first_octet == 169 and second_octet == 254:
		valid_ip = False

	for octet in (second_octet, third_octet, fourth_octet):
		if (octet < 0) or (octet > 255):
			valid_ip = False

All that is left now is to exit the script and print the IP address if it’s a valid IP, if it’s not print that it’s not a valid IP and start the While loop from the top again.

if valid_ip:
		not_done = False
	else:
		print("\nThe IP address entered was not valid, please try again.")

print("\n\nThe IP address is valid: {}".format(ip_add))

Running the script then looks like this.

[email protected]:~/python/Week4$ python3 ip_checker_enhanced.py 


Please enter an IP address: 192.168.1.1.1

Sorry but you didn't put in four octets. Please try again.


Please enter an IP address: 192.168.1

Sorry but you didn't put in four octets. Please try again.


Please enter an IP address: 169.254.0.1

The IP address entered was not valid, please try again.


Please enter an IP address: 172.16.0.1


The IP address is valid: 172.16.0.1

I hope this has been informative and code is at Github as always.

Python – Kirk Byers Course Week 4 Part 1

One thought on “Python – Kirk Byers Course Week 4 Part 1

  • September 15, 2017 at 2:55 am
    Permalink

    ”’
    if valid_ip:
    not_done = False
    else:
    print(“\nThe IP address entered was not valid, please try again.”)
    ”’
    Is continue statment missing after this else?
    If the else condition is hit here- Don’t you think the continue statement is needed for the loop to go again back up to the top?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: