Monday, October 17, 2016

Validating IP Address By Using Python (Conditional Statement)

Splitting IP Address string variable post defines the normal way how to deal with the variables and splitting them into different items. The same splitting can be used by using conditional statement if-else with while loop. This provides more simplicity for network engineers to shorten the python code.

Syntax of If-Else Statement:
if expression:

If expression is FALSE, in that case complier will move to the ELIF part. If ELIF becomes false also, the compiler will execute else block. The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

while True:
  ip_address = raw_input("Enter IP Address")
  ip_address_check = ip_address.split('.')

  if (len(ip_address_check) == 4) and (1 <= int(ip_address_check[0]) <= 223) and (int(ip_address_check[0]) != 127) and (int(ip_address_check[0]) != 169 or int(ip_address_check[1]) != 254) and (0 <= int(ip_address_check[1]) <= 255 and 0 <= int(ip_address_check[2]) <= 255 and 0 <= int(ip_address_check[3]) <= 255):

  print "\nThe IP address is INVALID! Please retry!\n"

print "Shivlu Jain It's Good to go with the given IP Address"

For more info on programming skill read Do I need to be programmer before learning Automation, SDN and NFV technologies?

People who read this post also read :

No comments: