# Twisted — Event-Driven Networking Engine for Python > Twisted is a mature event-driven networking framework for Python that supports TCP, UDP, TLS, HTTP, SMTP, SSH, DNS, and many other protocols in a single cohesive library. ## Install Save as a script file and run: # Twisted — Event-Driven Networking Engine for Python ## Quick Use ```bash pip install twisted ``` ```python from twisted.internet import reactor, protocol class Echo(protocol.Protocol): def dataReceived(self, data): self.transport.write(data) class EchoFactory(protocol.Factory): def buildProtocol(self, addr): return Echo() reactor.listenTCP(8000, EchoFactory()) reactor.run() ``` ## Introduction Twisted is one of the oldest and most comprehensive async networking frameworks in the Python ecosystem. Started in 2002, it provides protocol implementations for dozens of network protocols and a battle-tested event loop that predates Python's built-in asyncio by over a decade. ## What Twisted Does - Implements clients and servers for HTTP, SMTP, IMAP, SSH, DNS, IRC, and more - Runs an event-driven reactor that handles thousands of concurrent connections - Provides Deferred objects for composable asynchronous callback chains - Includes a test framework (Trial) specifically designed for testing async code - Supports TLS/SSL natively with certificate verification and SNI ## Architecture Overview Twisted is built around a reactor pattern. The reactor is a central event loop that monitors file descriptors, timers, and signals, dispatching events to protocol handlers. Protocols are layered on top of transports, cleanly separating parsing logic from I/O mechanics. The Deferred abstraction provides chainable callbacks and error handlers, similar to Promises in JavaScript. Modern Twisted also supports `async/await` via `ensureDeferred` and integrates with asyncio's event loop. ## Self-Hosting & Configuration - Install with `pip install twisted` or specific extras like `twisted[tls]` - Use `twistd` to run Twisted applications as daemons with logging and PID management - Write `.tac` application files to define long-running services - Enable TLS with `twisted.internet.ssl` and provide certificate chain files - Deploy behind a reverse proxy for HTTP applications or run standalone for protocol servers ## Key Features - Protocol implementations for over 20 network protocols in a single library - The Deferred system provides structured error propagation across async chains - Trial test runner handles async test cases with reactor cleanup between tests - Conch module provides a full SSH client and server implementation in pure Python - Incremental module provides composable, streaming data parsers ## Comparison with Similar Tools - **asyncio** — stdlib event loop with fewer built-in protocols, newer API - **Tornado** — async web framework focused on HTTP, narrower protocol support - **gevent** — monkey-patches blocking I/O, different concurrency model - **Trio** — modern structured concurrency library, less protocol coverage - **aiohttp** — HTTP client/server for asyncio, no multi-protocol support ## FAQ **Q: Should I use Twisted or asyncio for new projects?** A: For simple HTTP or WebSocket services, asyncio with a framework like FastAPI is often simpler. Twisted remains the better choice when you need implementations of non-HTTP protocols (SSH, SMTP, DNS) or need the mature Deferred-based error handling. **Q: Can Twisted work with asyncio?** A: Yes. Twisted can run on the asyncio event loop via `asyncioreactor`, and `ensureDeferred` lets you use `async/await` syntax within Twisted code. The two ecosystems interoperate well. **Q: Is Twisted still maintained?** A: Yes. Twisted has an active maintainer team and regular releases. It is used in production by large organizations for protocol servers, IoT gateways, and network tooling. **Q: What is a Deferred?** A: A Deferred is Twisted's equivalent of a Promise. It represents a value that will be available in the future, with methods to attach success callbacks and error handlers in a chain. ## Sources - https://github.com/twisted/twisted - https://twisted.org --- Source: https://tokrepo.com/en/workflows/3fee715a-442c-11f1-9bc6-00163e2b0d79 Author: Script Depot