GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Socket-Programming-Python/tcp_server.py at main · nihahawas/Socket-Programming-Python Expand file tree/ Copy pathtcp_server.pyMore file actions 95 lines (74 loc) · 2.81 KB / Copy pathtcp_server.pyFile metadata and controls95 lines (74 loc) · 2.81 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 """ Lab 06 — TCP Server Program Tasks Covered: - Task 1: TCP Server and Client communication - Task 3: Exchange at least five messages - Task 4: Send Student Name and Registration Number Bonus: - Timestamp in messages - Convert received message to UPPERCASE - Terminate when user types 'exit' """ import socket import datetime # ───────────────────────────────────────── # STUDENT INFO — EDIT THESE # ───────────────────────────────────────── STUDENT_NAME = "Niha Hawas" REG_NUMBER = "2312274" # ───────────────────────────────────────── def get_timestamp(): return datetime.datetime.now().strftime("[%H:%M:%S]") # Create TCP Socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Allow reuse of port immediately after restart server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) host = "127.0.0.1" port = 12345 # Bind IP and Port server_socket.bind((host, port)) # Listen for one incoming connection server_socket.listen(1) print("=" * 50) print(" TCP SERVER STARTED") print(f" Student : {STUDENT_NAME}") print(f" Reg No : {REG_NUMBER}") print("=" * 50) print(f"Listening on {host}:{port} ...") print("Waiting for client to connect...\n") conn, addr = server_socket.accept() print(f"✅ Connected with: {addr}\n") # Send student info to client on connect intro = f"SERVER CONNECTED | Student: {STUDENT_NAME} | Reg: {REG_NUMBER}" conn.send(intro.encode()) msg_count = 0 while True: try: data = conn.recv(1024).decode() if not data: print("Client disconnected.") break msg_count += 1 ts = get_timestamp() # Bonus: Convert to UPPERCASE before displaying upper_data = data.upper() print(f"{ts} Client [{msg_count}]: {data} → UPPERCASE: {upper_data}") # Bonus: Terminate if client sent 'exit' if data.strip().lower() == "exit": print("Client sent 'exit'. Closing connection.") conn.send("Server: Goodbye!".encode()) break reply = input("Your Reply (type 'exit' to quit): ").strip() # Bonus: Add timestamp to reply full_reply = f"{ts} [{STUDENT_NAME} | {REG_NUMBER}] Server: {reply}" conn.send(full_reply.encode()) # Bonus: Terminate server if server types 'exit' if reply.lower() == "exit": print("You ended the session.") break except ConnectionResetError: print("Connection was reset by client.") break conn.close() server_socket.close() print(f"\n✅ Session ended. Total messages exchanged: {msg_count}") |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |