A simple yet robust Python socket server that supports multiple clients, handles connection timeouts, and shuts down after periods of inactivity.
- Multi-client support using
threading - Idle client timeout handling
- Server auto-shutdown after inactivity
- Graceful disconnect handling
- Clean & structured code for easy learning
The server:
- Binds to
0.0.0.0and listens for incoming TCP connections. - Accepts multiple clients concurrently via threading.
- Closes idle connections after a set timeout (default: 20s per client).
- Shuts down after 120s of inactivity with warnings at 60s.
- Sends confirmation for each received message.
The client:
- Connects to the server using the server’s IP and port.
- Sends messages with a header indicating their length.
- Disconnects when sending
"disconnected"or an empty message.
python3 servercp.pyServer will start listening for incoming connections on port 5060.
python3 clientcp.pyEnter your messages. Send disconnected or leave the message empty to disconnect.
You can change these in the code:
PORT – Listening port (default: 5060)
HEADER – Message length header size (default: 64)
-
TLS/SSL encryption for secure communication
-
IP whitelisting / blacklisting
-
Authentication before message exchange
-
Logging of all incoming/outgoing traffic
Nour Brahim
📌 GitHub: https://github.com/NourBrahim
💼 LinkedIn: www.linkedin.com/in/nour-brahim777