From 32e9f16115d031fb001cd202e1458a9f61098224 Mon Sep 17 00:00:00 2001 From: "Er.Adam Eehan" Date: Wed, 11 Nov 2026 14:17:18 +0530 Subject: [PATCH] Add UDP echo server and client implementation This script implements a UDP echo server and client. The server listens on port 9001 and echoes back any received messages. --- sockets/python/udp_echo.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 sockets/python/udp_echo.py diff --git a/sockets/python/udp_echo.py b/sockets/python/udp_echo.py new file mode 100644 index 0000000..bcdc8dd --- /dev/null +++ b/sockets/python/udp_echo.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +"""UDP echo server (run with 'server') or client (run with 'client HOST').""" +import socket, sys + +PORT = 9001 +if len(sys.argv) > 1 and sys.argv[1] == "client": + host = sys.argv[2] if len(sys.argv) > 2 else "127.0.0.1" + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.settimeout(2) + s.sendto(b"hello", (host, PORT)) + print(s.recvfrom(1024)) +else: + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.bind(("0.0.0.0", PORT)); print(f"UDP echo on {PORT}") + while True: + data, addr = s.recvfrom(1024); s.sendto(data, addr)