A lightweight Python server implementation that provides both HTTP and UDP server functionality with multi-threading support and long connection capabilities, mainly used for setting up test environments for functional testing.
- Multi-threaded Processing: Each request is handled by a separate thread
- Long Connection Support: HTTP Keep-Alive support
- JSON API: RESTful API that accepts and returns JSON data
- Request Logging: Detailed logging of client IP, thread ID, and request information
- Error Handling: Comprehensive error handling with appropriate HTTP status codes
- Response Delay: Configurable delay in milliseconds for testing timeouts
- JSON Response Server: Receives UDP messages and sends back JSON confirmation
- Client Information: Returns client IP and port information
- Structured Response: Consistent JSON format with HTTP server
- Response Delay: Configurable delay in milliseconds for testing timeouts
- Python 3.6+
Start servers using the unified entry point:
# Start HTTP server (default port 8000)
python simple-server.py http
# Start HTTP server on custom port
python simple-server.py http 8080
# Start HTTP server without JSON validation
python simple-server.py http 8080 --no-json
# Start HTTP server with response delay
python simple-server.py http --delay 1000
python simple-server.py http 8080 --delay 500
# Start UDP server (default port 9000)
python simple-server.py udp
# Start UDP server on custom port
python simple-server.py udp 9999
# Start UDP server with response delay
python simple-server.py udp --delay 1000
python simple-server.py udp 9999 --delay 500
# Show help
python simple-server.py -hStart the HTTP server with default port 8000:
python http/http_server.pyOr specify a custom port:
python http/http_server.py 8080Disable JSON validation for POST requests:
python http/http_server.py --no-json
python http/http_server.py 8080 --no-jsonShow help information:
python http/http_server.py -h
python udp/udp_server.py --helpRequest Examples:
GET request:
curl http://localhost:8000/POST request with JSON data:
curl -X POST http://localhost:8000/api/test \
-H "Content-Type: application/json" \
-d '{"message": "Hello World", "data": [1, 2, 3]}'Start the UDP server with default port 9000:
python udp/udp_server.pyOr specify a custom port:
python udp/udp_server.py 9999UDP Client Example:
# Send default JSON message
python udp/udp_client.py 127.0.0.1 9000
# Send custom message
python udp/udp_client.py 127.0.0.1 9000 "Hello World"
# Send JSON message
python udp/udp_client.py 127.0.0.1 9000 '{"test": "data"}'
# Show help
python udp/udp_client.py -hsimple-server/
├── simple-server.py # Unified entry point
├── http/
│ └── http_server.py # Multi-threaded HTTP server
├── udp/
│ ├── udp_server.py # Simple UDP echo server
│ └── udp_client.py # UDP client for testing
└── README.md # This file
Both HTTP and UDP servers return consistent JSON responses with the following fields:
status: Request status ("success" or "error")message: Response messageclient_ip: Client IP addressclient_port: Client port numbertime: Human-readable timestampreceived_data: Data received from client (HTTP POST and UDP)path: Request path (HTTP GET only)
Success Response:
{
"status": "success",
"message": "Welcome to the long connection server",
"client_ip": "127.0.0.1",
"client_port": 54321,
"time": "Mon Jan 1 12:00:00 2022"
}Error Response:
{
"status": "error",
"message": "Request body is empty"
}The UDP server sends back a JSON response:
{
"status": "success",
"message": "UDP request processed successfully",
"received_data": "{\"test\": \"data\"}",
"client_ip": "127.0.0.1",
"client_port": 60322,
"time": "Tue Sep 30 18:07:22 2025"
}Both servers can be stopped by pressing Ctrl+C. They will gracefully shut down and display a shutdown message.