Twisted Web
Event-driven Python web server framework.
Overview
Twisted Web is a mature event-driven networking framework written in Python that pioneered asynchronous web development before async/await became mainstream. Originally developed by Glyph Lefkowitz in 2000, Twisted uses a reactor pattern and Deferred objects to handle thousands of concurrent connections efficiently in a single-threaded event loop, making it ideal for high-concurrency applications like chat servers, streaming services, and real-time data feeds. This Docker configuration sets up a Python 3.12 environment running Twisted Web server on port 8080, providing a lightweight foundation for building scalable network applications that require non-blocking I/O operations and long-lived connections. Twisted Web excels in scenarios where traditional threaded web servers struggle, particularly for applications requiring WebSocket connections, Server-Sent Events, or custom protocols alongside standard HTTP handling. The framework's unique approach to asynchronous programming through its reactor pattern and Deferred callbacks makes it particularly valuable for developers building chat applications, IoT data collectors, real-time dashboards, or any service requiring persistent client connections with minimal resource overhead.
Key Features
- Event-driven architecture with reactor pattern for handling thousands of concurrent connections
- Built-in WebSocket support through txws and autobahn integration for real-time communication
- WSGI compatibility allowing Django and Flask applications to run within Twisted's event loop
- Deferred objects for elegant callback-based asynchronous programming without async/await syntax
- Resource-based URL routing system with hierarchical request handling
- Integrated HTTP/HTTPS server with support for virtual hosts and SSL/TLS termination
- Protocol abstraction layer supporting custom network protocols beyond HTTP
- Template system integration with Nevow and Jinja2 for dynamic content generation
Common Use Cases
- 1Real-time chat applications requiring persistent WebSocket connections for multiple users
- 2IoT data collection services handling continuous sensor data streams from embedded devices
- 3Live dashboard applications displaying real-time metrics with Server-Sent Events
- 4Game servers requiring custom protocols and low-latency client-server communication
- 5API gateways for microservices requiring connection pooling and request multiplexing
- 6Streaming media servers delivering content to multiple concurrent clients
- 7Development environments for testing asynchronous Python applications before production deployment
Prerequisites
- Docker Engine 20.10+ and Docker Compose V2 for container orchestration support
- Minimum 512MB RAM for basic Twisted applications, 2GB+ for high-concurrency workloads
- Python development knowledge with understanding of callback-based asynchronous programming
- Port 8080 available on host system for Twisted Web server access
- Basic understanding of event-driven programming concepts and reactor patterns
- Knowledge of Twisted's Deferred objects and inlineCallbacks decorator usage
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 twisted: 3 image: python:3.12-slim4 container_name: twisted-web5 restart: unless-stopped6 working_dir: /app7 command: python server.py8 volumes: 9 - ./app:/app10 ports: 11 - "8080:8080"12 networks: 13 - twisted-network1415networks: 16 twisted-network: 17 driver: bridge.env Template
.env
1# Install twisted: pip install twistedUsage Notes
- 1Docs: https://docs.twisted.org/en/stable/web/
- 2Event-driven networking - single-threaded async model
- 3WebSocket support via autobahn or txws
- 4WSGI compatible - can run Django/Flask apps
- 5Good for real-time apps, chat, streaming
- 6Install in container: pip install twisted
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 twisted:5 image: python:3.12-slim6 container_name: twisted-web7 restart: unless-stopped8 working_dir: /app9 command: python server.py10 volumes:11 - ./app:/app12 ports:13 - "8080:8080"14 networks:15 - twisted-network1617networks:18 twisted-network:19 driver: bridge20EOF2122# 2. Create the .env file23cat > .env << 'EOF'24# Install twisted: pip install twisted25EOF2627# 3. Start the services28docker compose up -d2930# 4. View logs31docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/twisted-web/run | bashTroubleshooting
- ImportError: No module named 'twisted': Run 'pip install twisted' in container or add to requirements.txt
- reactor.run() called from non-main thread: Ensure Twisted reactor runs in main thread, use reactor.callFromThread() for threading
- Address already in use (port 8080): Check if another service uses port 8080 or modify port mapping in docker-compose.yml
- Unhandled error in Deferred: Add proper errback handlers to all Deferred objects to catch exceptions
- Resource not found (404 errors): Verify resource tree structure and ensure proper addChild() calls in resource hierarchy
- SSL context errors with HTTPS: Generate proper SSL certificates and configure SSL context factory in server.py
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download