A step-by-step TCP server/client implementation for educational purposes
Overview
This package demonstrates how a TCP client/server architecture works by providing a transparent, educational implementation using Python’s socket module. It’s built with nbdev to combine code, documentation, and examples in a single place, making it an excellent learning resource.
Install
pip install python_tcp
How to use
Here’s a simple example of a TCP echo server and client:
from python_tcp.server import TCPServerfrom python_tcp.client import TCPClientimport time# Start a serverserver = TCPServer(port=8000)server.start()# Connect with a clientclient = TCPClient()client.connect('127.0.0.1', 8000)# Send a messageclient.send(b"Hello, TCP world!")# Receive the responseresponse = client.receive()print(f"Received: {response.decode('utf-8')}")# Clean upclient.close()server.stop()
Server started on 127.0.0.1:8000
Connecting to 127.0.0.1:8000...
New connection from 127.0.0.1:34062 (ID: e1850ccc-b18d-4764-b925-5850a0f64bdd)
Connected to 127.0.0.1:8000
Received from e1850ccc-b18d-4764-b925-5850a0f64bdd: Hello, TCP world!
Received: Hello, TCP world!
Connection closed
Connection client-connection: ESTABLISHED -> CLOSED
Connection e1850ccc-b18d-4764-b925-5850a0f64bdd: ESTABLISHED -> CLOSED
Connection e1850ccc-b18d-4764-b925-5850a0f64bdd closed
Server socket closed
Connection e1850ccc-b18d-4764-b925-5850a0f64bdd: CLOSED -> CLOSED
Connection e1850ccc-b18d-4764-b925-5850a0f64bdd closed
Server stopped
Understanding TCP
TCP (Transmission Control Protocol) is a connection-oriented protocol that ensures reliable data delivery. This project demonstrates key TCP concepts:
The Three-Way Handshake
When establishing a connection, TCP uses a three-way handshake:
SYN: Client sends a SYN packet with sequence number x
SYN-ACK: Server responds with SYN-ACK (sequence y, acknowledgment x+1)
ACK: Client sends ACK (acknowledgment y+1)
TCP Socket States
TCP sockets go through various states: - CLOSED - LISTEN (server waiting for connections) - SYN_SENT (client initiated connection request) - SYN_RECEIVED (server received SYN) - ESTABLISHED (connection active) - FIN_WAIT (connection termination initiated) - CLOSE_WAIT (waiting for application to close) - TIME_WAIT (waiting for network cleanup)
Our implementation tracks and demonstrates these states.
Implementation Notes
This project takes care to avoid common pitfalls when using the socket library:
Naming conflicts: We use different attribute names (like sock instead of socket) to avoid confusion with the module name
Socket cleanup: We ensure proper socket closure in all scenarios, including error conditions
Thread safety: Our multithreaded design includes proper synchronization
Error handling: Comprehensive error handling for network operations
Project Components
This project contains several components:
Core: Basic TCP types and utilities
Server: TCP server implementations with various features
Client: TCP client implementations with various features
Examples: Practical demonstrations
Chat Application: A complete TCP-based chat application
Server Types
We provide several server implementations:
TCPServer: Basic TCP server that echoes messages
EnhancedTCPServer: Server with custom message handling
EventDrivenTCPServer: Server with event callbacks
Client Types
The client implementations mirror the server capabilities:
TCPClient: Basic TCP client
AsyncTCPClient: Client with asynchronous message reception
EventDrivenTCPClient: Client with event callbacks
Educational Focus
This package is designed for educational purposes to understand:
How TCP connections are established and closed
The client/server model
Socket programming
Event-driven networking
Asynchronous I/O
Each component includes detailed documentation explaining the underlying TCP concepts.
Limitations
As an educational tool, this implementation has some limitations:
Not optimized for production use
Limited error handling compared to production libraries
socketserver: Python’s standard library for creating network servers
twisted: Event-driven networking engine
tornado: Web framework and asynchronous networking library
Developer Guide
If you are new to using nbdev here are some useful pointers to get you started.
Install python_tcp in Development mode
# make sure python_tcp package is installed in development mode$ pip install -e .# make changes under nbs/ directory# ...# compile to have changes apply to python_tcp$ nbdev_prepare
Documentation can be found hosted on this GitHub repository’s pages. Additionally you can find package manager specific guidelines on conda and pypi respectively.