Fundamentals 10 min read

OSI, IP, UDP Overview and Building a Local UDP Server/Client with Python

This article introduces the OSI model, IP and UDP protocols, explains the structure of IP and UDP packet headers, demonstrates how to create a simple UDP server and client in Python, and shows how to capture and analyze the traffic with Wireshark.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
OSI, IP, UDP Overview and Building a Local UDP Server/Client with Python

The author recently explored Python networking and found that creating a local UDP server is an easy way to analyze UDP request and response traffic. This article first introduces the OSI model, the IP protocol, and the UDP protocol.

In the OSI seven‑layer model, UDP resides in the transport layer (layer 4) and IP resides in the network layer (layer 3). The article shows the OSI/TCP‑IP mapping diagram and explains that UDP is an unreliable datagram protocol while IP provides a datagram service for both TCP and UDP.

Next, the structure of the IP packet header is described in detail, covering fields such as version, header length, service type, total length, identifier, flags, fragment offset, TTL, protocol (0x11 for UDP), header checksum, source address (127.0.0.1), and destination address (127.0.0.1). Each field is illustrated with diagrams and the corresponding binary/hexadecimal values.

The UDP header is then examined. It occupies 8 bytes (64 bits) and contains source port, destination port, length, and checksum. The article provides diagrams for each field and explains the meaning of the values (e.g., source port 0xf432 = 62514, destination port 0x4e20 = 20000, length 0x0009 = 9 bytes, checksum 0xfe1c).

After the protocol analysis, the author presents a complete Python implementation of a UDP server using socketserver.BaseRequestHandler and socketserver.UDPServer , and a client using the low‑level socket API. The server prints the client address, received message, and sends back the response "ABCD". The client sends the character "A" to the server.

from socketserver import BaseRequestHandler, UDPServer

class handleRequest(BaseRequestHandler):
    def handle(self):
        print('Got connection from', self.client_address)
        msg, sock = self.request
        print('RequestMessage:', msg)
        resp = 'ABCD'
        print('Response:', resp)
        sock.sendto(resp.encode('ascii'), self.client_address)

if __name__ == '__main__':
    serv = UDPServer(('', 20000), handleRequest)
    serv.serve_forever()
from socket import socket, AF_INET, SOCK_DGRAM
s = socket(AF_INET, SOCK_DGRAM)
s.sendto('A', ('localhost', 20000))

The author captures the traffic with Wireshark, showing the request packet (data 0x41 = 65) and the response packet (data 0x41424344 = "ABCD"). Detailed explanations of each header field are provided alongside the captured images.

Finally, the article lists reference links for further reading on Wireshark packet analysis, IANA address families, and Python UDP server examples.

PythonWiresharkIPNetwork ProgrammingUDPPacket AnalysisOSI model
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.