-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10_network_packet_model.py
More file actions
55 lines (46 loc) · 1.63 KB
/
Copy path10_network_packet_model.py
File metadata and controls
55 lines (46 loc) · 1.63 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import re
class DataPacket:
"""
Demonstrating property decorators for strict IP validation.
Ensures that the source and destination IP addresses follow the standard IPv4 format.
"""
def __init__(self, source_ip, dest_ip, data):
self.source_ip = source_ip
self.dest_ip = dest_ip
self.data = data
@property
def source_ip(self):
return self._source_ip
@source_ip.setter
def source_ip(self, value):
if not self._validate_ip(value):
raise ValueError(f"Invalid Source IP format: {value}")
self._source_ip = value
@property
def dest_ip(self):
return self._dest_ip
@dest_ip.setter
def dest_ip(self, value):
if not self._validate_ip(value):
raise ValueError(f"Invalid Destination IP format: {value}")
self._dest_ip = value
@staticmethod
def _validate_ip(ip):
"""Helper method to validate IPv4 address format."""
pattern = r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$"
if re.match(pattern, ip):
# Check if each octet is between 0 and 255
return all(0 <= int(octet) <= 255 for octet in ip.split('.'))
return False
def __str__(self):
return f"Packet: [{self.source_ip} -> {self.dest_ip}] Data: {self.data}"
# Testing the implementation
if __name__ == "__main__":
try:
# Valid packet
p1 = DataPacket("192.168.1.1", "10.0.0.5", "Hello World")
print(p1)
# Invalid packet (invalid IP format)
p2 = DataPacket("256.0.0.1", "10.0.0.5", "Error Test")
except ValueError as e:
print(f"Validation Error: {e}")