A hands-on demonstration of socket programming in Python, showcasing both TCP and Unix domain socket implementations.
A socket is an endpoint for network communication between processes. It acts as an interface between the application layer and the transport layer of the network stack. Sockets can be used for both local (inter-process) and network communication.
Defines the addressing scheme and protocol family used by the socket:
- AF_INET: IPv4 Internet protocols (TCP/UDP over IPv4)
- AF_UNIX: Local communication (Unix domain sockets)
Unix domain sockets use the file system as their address namespace.
A integer that serves as an index into the kernel's file descriptor table.
This project demonstrates how to create a simple HTTP server using different socket types:
- TCP Sockets: Network-based communication using IP addresses and ports
- Unix Domain Sockets: File-based communication using socket files
- Simple HTTP server that responds to GET requests
- Support for both TCP and Unix domain sockets
- Graceful shutdown handling with signal management
- Clean socket file management for Unix domain sockets
- Basic HTTP request parsing and response generation
local_server/
├── server.py # HTTP server implementation
├── request.py # HTTP client for testing
Run the server with either TCP or Unix domain socket:
# Start with TCP socket (listens on localhost:8080)
python3 server.py --socket tcp
# Start with Unix domain socket (uses /tmp/local_server.sock)
python3 server.py --socket unixUse the client to test the server:
# Test TCP server
python3 request.py --socket tcp
# Test Unix domain socket server
python3 request.py --socket unix- The
--socketparameter must match between client and server - TCP server binds to
localhost:8080 - Unix domain socket uses
/tmp/local_server.sock - Server responds with "running" for GET requests
- Other HTTP methods return "Method not allowed"
- Uses
AF_INETaddress family - Binds to
localhost:8080 - Accepts connections from any client that can reach the port
- Returns client address as
(host, port)tuple
- Uses
AF_UNIXaddress family - Binds to socket file
/tmp/local_server.sock - Only accessible from the same machine
- Returns empty string for client address
- Automatically cleans up socket file on shutdown
✅ Server running. File descriptor: 3
📨 New TCP connection from: ('127.0.0.1', 54321)
✅ Server running. File descriptor: 3
📨 New Unix domain socket connection
args:Namespace(socket='tcp')
[+] Response: HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 7
Connection: close
running
The server gracefully handles shutdown signals:
SIGINT(Ctrl+C)SIGTERM
When terminated, it:
- Closes the server socket
- Cleans up Unix socket file (if applicable)
- Exits cleanly
- Socket creation failures are caught and reported
- Invalid socket types raise descriptive exceptions
- Connection errors are handled gracefully
- Socket file cleanup on startup and shutdown
This is a learning example demonstrating:
- Basic socket programming concepts
- HTTP request/response handling
- Different socket types and their characteristics
- Proper resource cleanup and signal handling