-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_valid_ipv4.py
More file actions
27 lines (22 loc) · 850 Bytes
/
test_valid_ipv4.py
File metadata and controls
27 lines (22 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#A valid IPv4 address is a string that contains
# four decimal numbers separated by periods,
# where each decimal number is between 0 and 255.
def is_valid_IPv4(ip_address):
# Split the IP address string into a list of 4 decimal numbers
parts = ip_address.split(".")
# Check that there are exactly 4 parts
if len(parts) != 4:
return False
# Check that each part is an integer between 0 and 255
for part in parts:
try:
# Convert each part to an integer
num = int(part)
except ValueError:
# If a part cannot be converted to an integer, the address is not valid
return False
# Check that the integer is between 0 and 255
if num < 0 or num > 255:
return False
# If all checks pass, the address is valid
return True