Esta página se muestra en inglés. Una traducción al español está en curso.
SkillsApr 30, 2026·3 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Twisted Overview
Comando de instalación directa
npx -y tokrepo@latest install 3fee715a-442c-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados