diff --git a/task1.py b/task1.py new file mode 100644 index 0000000000000000000000000000000000000000..d2044f46064151ee03345a90f943bef436fc912d --- /dev/null +++ b/task1.py @@ -0,0 +1,48 @@ +import socket +import base64 + +# create a UDP socket +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +# connect to the server +server_address = ('localhost', 5612) +sock.connect(server_address) + +while True: + # receive a message from the server + data, server = sock.recvfrom(4096) + + if data: + # print the base64 encoded packet + print(f'Base64: {data}') + + # decode the base64 encoded packet + packet = base64.b64decode(data) + + # print the decoded packet + print(f'Server Sent: {packet}') + + # extract the fields from the header + source_port = int.from_bytes(packet[0:2], 'little') + dest_port = int.from_bytes(packet[2:4], 'little') + length = int.from_bytes(packet[4:6], 'little') + checksum = int.from_bytes(packet[6:8], 'little') + + # print the fields + print('Decoded Packet:') + print(f'Source Port: {source_port}') + print(f'Dest Port: {dest_port}') + print(f'Data Length: {length}') + print(f'Checksum: {checksum}') + + # extract and decode the payload + payload = packet[8:length].decode('utf-8') + + # print the payload + print(f'Payload: {payload}') + + else: + break + +# close the socket +sock.close()