From a45d036a9cdce1bac362e6efc5a1dc8f624263d6 Mon Sep 17 00:00:00 2001 From: Aya-Jafar Date: Tue, 12 Jul 2022 15:54:16 +0300 Subject: [PATCH 1/2] =?UTF-8?q?Task1=20=E2=9C=94=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 71a3166..8f37c5d 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,89 @@ def solution(): - pass + while True: + ip = input('Enter an IP address in the format x.x.x.x/x :\n') + if is_valid(ip): + ip_octets = split_ip(ip) + ip_octets = [int(i) for i in ip_octets] + ip_class = find_class(ip_octets[0]) + ip_designation = find_designation(ip_octets) + print(f'Class: {ip_class}, Designation: {ip_designation}') + break + + else: + print('Please try again...') + continue + + +def is_valid(str_ip): + ip = split_ip(str_ip) + + if len(ip) != 4: + print('The number of octets should equel four') + return False + + for i in ip: + if (not i.isdigit()): + print('All octets should be numeric') + return False + elif (i.isdigit() and int(i) not in range(0, 256)): + print('All ocetets should be in range 0-255') + return False + + cidr_index = str_ip.find('/')+1 + if int(str_ip[cidr_index:]) not in range(0, 33): + print('CIDR notation should be between 0-32') + return False + + return True + + +def split_ip(str_ip): + ip_octets = str_ip.split('/')[0].split('.') + return ip_octets + + +def find_class(first_octete): + if first_octete in range(1, 128): + return 'A' + if first_octete in range(128, 192): + return 'B' + if first_octete in range(192, 224): + return 'C' + if first_octete in range(224, 240): + return 'D' + if first_octete in range(240, 256): + return 'E' + + +def find_designation(ip_octets): + '''' + Private IP : + 10.0.0.0 — 10.255.255.255 + 169.254.0.0 - 169.254.255.255 + 172.16.0.0 - 172.31.255.255 + 192.168.0.0 - 192.168.255.255 + Special IP : + 127.0.0.1 - 127.255.255.255 + Any range else : + Public IP + ''' + if any ([ + ip_octets[0] == 10 , + ip_octets[0] == 169 and ip_octets[1] == 254 , + ip_octets[0] == 172 and ip_octets[1] in range(16, 32) , + ip_octets[0] == 192 and ip_octets[1] == 168 + ]): + return 'Private' + + elif all ([ + ip_octets[0] == 127 , + ip_octets[-1] in range(1, 256) + ]): + return 'Special' + + else: + return 'Public' if __name__ == '__main__': - pass + solution() From 826d1a0f8bd44d88de74d6c571222bd218088fde Mon Sep 17 00:00:00 2001 From: Aya-Jafar Date: Wed, 13 Jul 2022 17:45:42 +0300 Subject: [PATCH 2/2] =?UTF-8?q?Task1=20=E2=9C=94=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/main.py b/main.py index 8f37c5d..3983cc1 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ +import re + def solution(): while True: - ip = input('Enter an IP address in the format x.x.x.x/x :\n') + ip = input('Enter an IP address in the format #.#.#.#/# :\n') if is_valid(ip): ip_octets = split_ip(ip) ip_octets = [int(i) for i in ip_octets] @@ -8,27 +10,30 @@ def solution(): ip_designation = find_designation(ip_octets) print(f'Class: {ip_class}, Designation: {ip_designation}') break - else: - print('Please try again...') + print('Please try again...\n') continue def is_valid(str_ip): - ip = split_ip(str_ip) + pattern = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}") + checker = pattern.match(str_ip) - if len(ip) != 4: - print('The number of octets should equel four') + if not checker: + print('The IP address does not match the required format') return False + ip = split_ip(str_ip) + for i in ip: - if (not i.isdigit()): - print('All octets should be numeric') - return False - elif (i.isdigit() and int(i) not in range(0, 256)): + if (int(i) not in range(0, 256)): print('All ocetets should be in range 0-255') return False + if len(i) > len(i.lstrip('0')) and len(i) != 1: + print('Leading zeros in any octet are not allowed') # 01 or 001 + return False + cidr_index = str_ip.find('/')+1 if int(str_ip[cidr_index:]) not in range(0, 33): print('CIDR notation should be between 0-32') @@ -56,17 +61,6 @@ def find_class(first_octete): def find_designation(ip_octets): - '''' - Private IP : - 10.0.0.0 — 10.255.255.255 - 169.254.0.0 - 169.254.255.255 - 172.16.0.0 - 172.31.255.255 - 192.168.0.0 - 192.168.255.255 - Special IP : - 127.0.0.1 - 127.255.255.255 - Any range else : - Public IP - ''' if any ([ ip_octets[0] == 10 , ip_octets[0] == 169 and ip_octets[1] == 254 , @@ -75,10 +69,9 @@ def find_designation(ip_octets): ]): return 'Private' - elif all ([ - ip_octets[0] == 127 , - ip_octets[-1] in range(1, 256) - ]): + elif ( + ip_octets[0] == 127 + ): return 'Special' else: