An asynchronous networking framework written in Python
npx @tessl/cli install tessl/pypi-twisted@25.5.0An asynchronous networking framework written in Python that enables developers to build scalable network applications, servers, and clients. Twisted provides a rich ecosystem of protocols and services including HTTP clients/servers with WSGI support, SSH/Telnet clients/servers, IRC/XMPP messaging protocols, IMAP/POP3/SMTP mail services, DNS tools, GPS communication utilities, and a specialized unit testing framework (twisted.trial).
pip install Twistedimport twistedCommon imports for basic functionality:
from twisted.internet import reactor, defer, protocol, endpoints
from twisted.web import server, resource
from twisted.application import servicefrom twisted.internet import reactor, protocol
from twisted.internet.endpoints import TCP4ServerEndpoint
class EchoProtocol(protocol.Protocol):
def dataReceived(self, data):
self.transport.write(data)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return EchoProtocol()
# Create server endpoint and listen
endpoint = TCP4ServerEndpoint(reactor, 8080)
endpoint.listen(EchoFactory())
# Start the reactor event loop
reactor.run()Twisted is built around several key architectural patterns:
The framework supports all major system event loops (select, poll, epoll, kqueue, IOCP) and GUI event loops (GTK, Qt, wxWidgets), making it highly portable and suitable for integration into diverse environments.
Core event loop and asynchronous programming primitives including Deferreds, protocols, factories, and endpoints. The reactor provides the foundation for all network operations in Twisted.
# Key functions and classes from twisted.internet
def defer.succeed(result): ...
def defer.fail(failure): ...
def defer.inlineCallbacks(func): ...
class defer.Deferred: ...
class protocol.Protocol: ...
class protocol.Factory: ...
class endpoints.TCP4ServerEndpoint: ...Complete HTTP implementation with client agents, server resources, and web application framework. Includes support for WSGI applications, static file serving, and advanced HTTP features.
# Key classes from twisted.web
class server.Site: ...
class resource.Resource: ...
class client.Agent: ...
class client.Response: ...SSH version 2 protocol implementation with client, server, SFTP support, and terminal handling capabilities. Includes key management, authentication, and channel forwarding.
# Key classes from twisted.conch.ssh
class transport.SSHClientTransport: ...
class transport.SSHServerTransport: ...
class keys.Key: ...
class filetransfer.FileTransferClient: ...Comprehensive unit testing framework with asynchronous test support, specialized test runners, and integration with Python's unittest module.
# Key classes from twisted.trial
class unittest.TestCase: ...
class runner.TrialRunner: ...
class reporter.TreeReporter: ...Service-oriented application architecture with hierarchical service management, plugin system, and daemon utilities for building long-running applications.
# Key interfaces and classes from twisted.application
class service.IService: ...
class service.Service: ...
class service.MultiService: ...
class service.Application: ...Pluggable authentication and authorization system with support for various credential types, checkers, and realms for securing network services.
# Key classes from twisted.cred
class portal.Portal: ...
class credentials.UsernamePassword: ...
class checkers.ICredentialsChecker: ...Complete email protocol implementations including SMTP, POP3, and IMAP4 with support for both client and server operations.
# Key classes from twisted.mail
class smtp.SMTP: ...
class pop3.POP3: ...
class imap4.IMAP4Server: ...DNS protocol implementation with client resolvers, server functionality, and caching capabilities for domain name resolution.
# Key classes from twisted.names
class client.Resolver: ...
class dns.Message: ...
class dns.Record_A: ...Implementations of various network protocols including FTP, IRC, XMPP, AMP (Asynchronous Messaging Protocol), and utilities for building custom protocols.
# Key classes from twisted.protocols
class basic.LineReceiver: ...
class amp.AMP: ...
class ftp.FTPClient: ...Modern structured logging system with event-based architecture, filtering, and multiple output formats for comprehensive application logging.
# Key classes from twisted.logger
class Logger: ...
class LogPublisher: ...
class FileLogObserver: ...Perspective Broker (PB) system for distributed object communication and remote method calls across network boundaries.
# Key classes from twisted.spread.pb
class Referenceable: ...
class Root: ...
class PBClientFactory: ...Twisted uses several key exception types for error handling:
# Common exceptions from twisted.internet.error
class ConnectionDone(Exception): ...
class ConnectionLost(Exception): ...
class TimeoutError(Exception): ...
class ConnectionRefusedError(Exception): ...Most Twisted APIs use Deferred objects for error propagation:
from twisted.internet import defer
def handleError(failure):
# Handle the error
print(f"Error occurred: {failure.value}")
def handleSuccess(result):
# Handle successful result
print(f"Success: {result}")
d = someAsyncOperation()
d.addCallback(handleSuccess)
d.addErrback(handleError)# Basic installation
pip install Twisted
# With specific optional dependencies
pip install Twisted[tls,http2,conch]
# Development installation
pip install Twisted[dev]Core Dependencies:
Optional Dependencies: