Untitled
unknown
plain_text
8 months ago
2.0 kB
4
Indexable
import ipaddress
def get_ip_class(ip):
first_octet = int(ip.split('.')[0])
if 1 <= first_octet <= 126:
return 'Class A'
elif 128 <= first_octet <= 191:
return 'Class B'
elif 192 <= first_octet <= 223:
return 'Class C'
elif 224 <= first_octet <= 239:
return 'Class D'
elif 240 <= first_octet <= 255:
return 'Class E'
else:
return 'Invalid IP'
def get_ip_class_abc(ip):
first_octet = int(ip.split('.')[0])
if 1 <= first_octet <= 126:
return 'Class A'
elif 128 <= first_octet <= 191:
return 'Class B'
elif 192 <= first_octet <= 223:
return 'Class C'
else:
return 'Not in Class A, B, or C'
def ip_to_32bit(ip):
try:
packed_ip = int(ipaddress.IPv4Address(ip))
return f'{packed_ip:032b}'
except ipaddress.AddressValueError:
return 'Invalid IP Address'
def bit_stuffing(data):
stuffed = ''
count = 0
for bit in data:
if bit == '1':
count += 1
else:
count = 0
stuffed += bit
if count == 5:
stuffed += '0'
count = 0
return stuffed
def bit_de_stuffing(stuffed):
data = ''
count = 0
for bit in stuffed:
if bit == '1':
count += 1
else:
count = 0
data += bit
if count == 5:
if len(data) > 0 and data[-1] == '0':
data = data[:-1]
count = 0
return data
# Test cases
ips = ['192.168.1.1', '10.0.0.1', '172.16.0.1', '224.0.0.1', '255.255.255.255']
for ip in ips:
print(f'IP: {ip}, Class: {get_ip_class(ip)}')
print(f'IP: {ip}, Class ABC: {get_ip_class_abc(ip)}')
print(f'IP: {ip}, 32-bit format: {ip_to_32bit(ip)}')
bit_data = '011111101111110'
stuffed = bit_stuffing(bit_data)
de_stuffed = bit_de_stuffing(stuffed)
print(f'Original Data: {bit_data}')
print(f'Stuffed Data: {stuffed}')
print(f'De-stuffed Data: {de_stuffed}')
Editor is loading...
Leave a Comment