-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_addr_valid.py
More file actions
20 lines (17 loc) · 1.25 KB
/
ip_addr_valid.py
File metadata and controls
20 lines (17 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import sys
#Checking octets
def ip_addr_valid(list):
#for loop to iterate over the list of IP addresses and check if the IP address is valid or not
for ip in list:
#the strip() right here which is a string specfic method that strips a character from the right side of a string , we are getting rid of the new line character at the end of the string
ip = ip.rstrip("\n")
#we're also splitting each IP address in the list using the dot as a delimiter and then saving the list generated by the split method using the octet_list variable.
octet_list = ip.split('.')
if (len(octet_list) == 4) and (1 <= int(octet_list[0]) <= 223) and (int(octet_list[0]) != 127) and (int(octet_list[0]) != 169 or int(octet_list[1]) != 254) and (0 <= int(octet_list[1]) <= 255 and 0 <= int(octet_list[2]) <= 255 and 0 <= int(octet_list[3]) <= 255):
continue
else:
print('\n* There was an invalid IP address in the file: {} :(\n'.format(ip))
sys.exit()
# we also know unicast range we have two special cases:
#First the addresses starting with 127 are reserved for loopback addresses.
#Second the addresses starting with 169.254 are reserved for link local addresses.