Twisted Network Programming Essentials

This review was originally printed in the June 2013 edition of the UKUUG newsletter, which is now defunct.

Title: Twisted Network Programming Essentials (2nd edition)
Authors: Abe Fettig, Jessica McKellar
ISBN: 9781449326111

Event driven programming appears to be the current ‘in thing’, but I have never fully understood how it works. I picked up this book in search of enlightenment – particularly as I have wanted for some time to write a customised IRC bot for updating channel topics with the latest events. One important point to note is that this book, not unreasonably, assumes a prior understanding of Python – although I managed to get by with my limited knowledge and preference for C-style syntax. Twisted also only works under Python 2.x at present, although a port to Python 3 is in progress.

Chapter one is a very brief ‘getting started’ guide. Twisted is available for most Linux distributions, so installation is simple, and OS X users should find it pre-installed. There’s a brief outline of where to find the documentation and other forms of help (mailing lists, IRC etc.) and then you’re straight into the next chapter.

The second chapter drops you straight into code – no long preambles here. A simple echo client/server implementation demonstrates the basic structure of a Twisted application, including the Reactor, Transport and Protocols. Event-driven programming is introduced at this point, with a helpful diagram and explanation, contrasting it to more familiar paradigms such as single threaded and multithreaded.

Chapter three introduces the concept of Deferreds – a way of abstracting the management of callbacks. Multiple callbacks (and ‘errbacks’ for errors) can be registered with a single deferred. This topic would have benefited from a more detailed explanation – given how important it is to later chapters – but the diagrams were useful and the Twisted website can help fill in the gaps.

Chapter four examines the construction of a HTTP server, with examples for serving static and dynamic content. The detail is fairly limited though, and only just over a page is given to the asynchronous handling of requests – which is the whole point of using a library such as Twisted. This is followed by a chapter on the client side of web requests, which again only provides a fairly brief overview.

The next part of the book groups several chapters under the heading of ‘Building Production-Grade Twisted Servers’. Deployment, logging and databases are all covered in short order – I would have liked to have seen more detail on the latter topic given that database queries are usually blocking. The chapter on authentication is more useful, carefully defining all the components of the generic ‘Cred’ system which ships with Twisted, before providing multiple examples and a helpful process diagram. Testing receives the longest chapter in the book, with examples of unit tests for all the major components of a typical Twisted application.

The final part of the book consists of a collection of short chapters with varying degrees of implementation details for IRC, email and SSH clients and servers. The IRC chapter in particular provided a useful starting point for my bot.

Overall, this is a good introduction to the Twisted framework and event-driven programming for anyone who is already familiar with Python. It does concentrate on breadth rather than depth of coverage, so further reading is required in order to produce fully fledged clients or servers. No book can cover everything though, and I would recommend this as a good starting point before jumping into the more detailed documentation on the Twisted website.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.