December 2018
Intermediate to advanced
226 pages
4h 57m
English
This example will show us how to validate an IP address format, given as an input:
ip_address=input("Enter IP address: ")#remove any extra charactersip_address=ip_address.strip()#initialize a flag to point to true for an ip addressip_address_flag=True#validate if there are only 3 dots (.) in ip addressif (not(ip_address.count('.') == 3)): ip_address_flag=Falseelse: #Validate if each of the octet is in range 0 - 255 ip_address=ip_address.split(".") for val in ip_address: val=int(val) if (not(0 <= val <=255)): ip_address_flag=False#based upon the flag value display the relevant messageif (ip_address_flag): print ("Given IP is correct")else: print ("Given IP is not correct")
The sample output is as follows:
>> Enter ...
Read now
Unlock full access