A simple real-time chat application using Python's built-in HTTP server capabilities. No external dependencies required!
| File | Description |
|---|---|
server.py |
HTTP server that handles messages and serves a web UI |
client.py |
Command-line chat client |
INSTRUCTIONS.md |
This file |
Open a terminal and run:
python3 server.pyYou should see:
╔══════════════════════════════════════════════════════════╗
║ HTTP Chat Server Started ║
╠══════════════════════════════════════════════════════════╣
║ Server running at: http://localhost:8080 ║
...
You have two options to chat:
- Open your browser
- Go to: http://localhost:8080
- Enter a username and start chatting!
Open a new terminal and run:
python3 client.pyFollow the prompts to enter your username and start chatting.
| Command | Description |
|---|---|
/name <username> |
Change your display name |
/status |
Check server connection status |
/history |
View all messages |
/clear |
Clear the terminal screen |
/quit or /exit |
Exit the client |
Enter your username (or press Enter for 'Anonymous'): Alice
👋 Welcome, Alice!
Alice> Hello everyone!
Alice> /status
✅ Server: online
📊 Messages: 1
Alice> /quit
👋 Goodbye!
The server exposes these HTTP endpoints:
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Web-based chat interface |
GET |
/messages |
Get all messages (JSON) |
GET |
/messages?since=N |
Get messages after index N |
GET |
/status |
Server status check |
POST |
/send |
Send a new message |
Send a message:
curl -X POST http://localhost:8080/send \
-H "Content-Type: application/json" \
-d '{"username": "Bob", "message": "Hello from cURL!"}'Get all messages:
curl http://localhost:8080/messagesCheck server status:
curl http://localhost:8080/statusEdit server.py and modify the run_server() call at the bottom:
if __name__ == '__main__':
run_server(port=3000) # Change to your preferred portpython3 client.py http://192.168.1.100:8080- Python 3.6+ (No external packages needed!)
- Works on macOS, Linux, and Windows
┌─────────────────┐ HTTP ┌─────────────────┐
│ │◄────────────────────►│ │
│ Chat Server │ GET /messages │ Web Browser │
│ (server.py) │ POST /send │ (localhost) │
│ │ │ │
└────────┬────────┘ └─────────────────┘
│
│ HTTP
│
┌────────▼────────┐
│ │
│ CLI Client │
│ (client.py) │
│ │
└─────────────────┘
- Messages are stored in memory and will be lost when the server stops
- This is designed for learning purposes, not production use
- The web UI polls for new messages every 2 seconds
Open multiple terminals or browser tabs to simulate multiple users chatting with each other.