or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

application.mdasync-io.mdauth.mddistributed.mddns.mdemail.mdhttp.mdindex.mdlogging.mdprotocols.mdssh.mdtesting.md
tile.json

tessl/pypi-twisted

An asynchronous networking framework written in Python

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/twisted@25.5.x

To install, run

npx @tessl/cli install tessl/pypi-twisted@25.5.0

index.mddocs/

Twisted

An 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).

Package Information

  • Package Name: Twisted
  • Language: Python
  • Installation: pip install Twisted

Core Imports

import twisted

Common imports for basic functionality:

from twisted.internet import reactor, defer, protocol, endpoints
from twisted.web import server, resource
from twisted.application import service

Basic Usage

from 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()

Architecture

Twisted is built around several key architectural patterns:

  • Reactor Pattern: Central event loop handling I/O operations asynchronously
  • Deferred Objects: Promise-like objects for managing asynchronous operations
  • Protocol/Factory: Network protocol implementations with factory pattern for connection handling
  • Service Framework: Hierarchical service management for application components
  • Plugin System: Extensible architecture for adding functionality

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.

Capabilities

Asynchronous I/O and Reactors

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: ...

Asynchronous I/O

HTTP Client and Server

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: ...

HTTP Implementation

SSH and Terminal Protocols

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: ...

SSH and Terminal

Testing Framework

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: ...

Testing Framework

Application Framework

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: ...

Application Framework

Authentication and Authorization

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: ...

Authentication

Email Protocols

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: ...

Email Protocols

DNS Client and Server

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: ...

DNS Implementation

Network Protocols

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: ...

Network Protocols

Logging System

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: ...

Logging System

Distributed Objects

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: ...

Distributed Objects

Common Error Handling

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)

Installation and Dependencies

# Basic installation
pip install Twisted

# With specific optional dependencies
pip install Twisted[tls,http2,conch]

# Development installation
pip install Twisted[dev]

Core Dependencies:

  • zope.interface >= 5
  • constantly >= 15.1
  • incremental >= 24.7.0
  • Automat >= 24.8.0
  • hyperlink >= 17.1.1
  • attrs >= 22.2.0
  • typing_extensions >= 4.2.0

Optional Dependencies:

  • pyOpenSSL (for TLS/SSL support)
  • service_identity (for TLS certificate verification)
  • pycrypto (for SSH support)
  • pyasn1 (for SSH key formats)