Friday 1:55 p.m.–2:25 p.m.
An Introduction to Twisted
- Audience level:
- Python Libraries
Twisted is an event-driven, networking library. This talk aims to explain what that means and give an overview of some of the main Twisted concepts. It will also describe some of the functionality that Twisted offers out of the box.
Twisted is an event-driven networking library. The problem that Twisted is trying to solve is dealing with networking events in Python. Without Twisted, software would have to interact with sockets and deal with the fact that the results of networking operations are not immediately available. Twisted provides mechanisms so that software does not have to interpret socket operations and does not have to block while a network operation is in progress. This means that software can operate at a conceptually higher level of networking and can operate with a high degree of concurrency. Twisted provides some basic abstractions upon which it builds ands offers many options. The reactor is an event loop which monitors sockets and calls back into higher level Python code when events occur. Above the socket level, Twisted makes a distinction between transports and protocols. A transport represents a connection over which bytes are transported such as TCP connections or UNIX pipes. A protocol represents a networking protocol such as HTTP or SMTP. Protocols are paired with transports but by separating the concerns, Twisted makes it easy to mix and match. Protocol factories are used to create protocols when new connections are established. Deferreds are a mechanism to specify what should happen in the future when an event occurs. Upon these basic abstractions, Twisted has built a large library of code for a wide range of networking protocols. Twisted provides working implementations of many things such as HTTP, SMTP, POP, IMAP, FTP, IRC, SSH, DNS servers and clients. They can be used as the basis for further specialized applications or they can be run as standalone applications with the twistd utility.