UDP protocol with a header implementation in python

import socketsocket_object = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket_object.bind(("127.0.0.1", 1111))
data = “Hello world”
packet = data.encode()
receiver_addr = (“127.0.0.1”, 1112) # For now, it is just an arbitary address
socket_object.sendto(packet, receiver_addr)
import zlibdef checksum_calculator(data):
checksum = zlib.crc32(data)
return checksum
data = “Hello world”
packet = data.encode()
checksum = checksum_calculator(packet)
source_port = 1111
destination_port = 1112
data_length = len(packet)
checksum = checksum_calculator(packet)
udp_header = struct.pack(“!IIII”, source_port, destination_port, data_length, checksum)
packet_with_header = udp_header + packet
full_packet, sender_address = socket_object.recvfrom(1024)
udp_header = full_packet[:16]
data = full_packet[16:]
udp_header = struct.unpack("!IIII", udp_header)
correct_checksum = udp_header[3]
correct_checksum = udp_header[3]
checksum = checksum_calculator(data)
is_data_corrupted = correct_checksum != checksum

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abdella Solomon

Abdella Solomon

A 17yo Data Scientist | Programmer | Writer | Freelancer | Lecturer