by Brandon Rhodes • Home

Foundations of Python Network Programming: A Last Hurrah For Python 2

Date: 24 January 2011
Tags:books, computing, python
Book cover of Foundations of Python Network Programming

My long labor is at last at an end: Apress has published my rewrite of John Goerzen's Foundations of Python Network Programming (2004) and I have released the example programs as both Python 2 and Python 3 code in a Bitbucket repository and also as a zipfile on the Apress web site. I can finally return to more casual writing — this is my first blog post in many months! — and can be more active in Python projects again.

I was amazed, as I studied the book's popular first edition, at how far Python has come since 2004. None of the modern Python web frameworks existed; the book offered one chapter on CGI programming, another on mod_python, and never even mentioned Zope. Python 2.3 was the most recent version of Python. The book could cite FTP as one of the “most widely used protocols on the Internet.” Parsing HTML involved stepping through the markup with HTMLParser and the recommended approach to processing XML was the awkward xml.dom. JSON was not yet popular enough to even warrant mention. The author recommended the dependable syslog module over the newfangled logging package in the Standard Library, and remote administration tools like paramiko and fabric either did not exist or were not yet on the radar.

Looking back, it appears that 2005 — the year following the first edition's publication — was a turning point for Python web technology in particular. CherryPy saw its first release in late 2004, and over the next year we were introduced to Django, Pylons, and Turbogears, all on the heels of the Ruby on Rails “framework” phenomenon. And both of the web scraping technologies covered in my new edition of the book, BeautifulSoup and lxml, also came out in 2005. In the face of all these changes, the web chapters received a full rewrite based on the latest technology.

I also rewrote the introductory chapters so that UDP and TCP each get their own chapter, focused on the protocol itself, instead of leaving the information about each protocol split between a client socket chapter and a server socket chapter. I have replaced the separate chapters on forking, threading, and event-driven servers with a single chapter that takes an example network server, rewrites that same server six different ways — including Twisted and a raw polling event loop — and then uses funkload to study the resulting performance. And even though an editor objected that it lacked unity, I insisted that a chapter be dedicated to memcached, message queues, and map-reduce, since they are so important to scaling modern Internet services.

Apress had already slated the project for Python 2 based on the advice of a well-known Python programmer, and I heartily supported his recommendation. His wisdom is bourne out by the result: as you can see from the README.txt that I included with the source code bundle, fully one-quarter (22/86) of the programs will simply not run today under Python 3 because of missing dependencies. I see this new Foundations of Python Network Programming as the perfect companion for Python 2.7 — which was released as I was writing — since the book will bring you up to date with the final features of the Python 2 series and the third party libraries that support it.

Type size comparison

I enjoyed working with the Apress team; Laurin Becker is a great managing editor who was always on my side, and Michael Bernstein as technical editor gave great feedback which made this an even better book. There were only two annoyances that I encountered with the book's release. First, as the accompanying image illustrates, Apress used a much more compact layout than in the first edition. This not only makes the book appear much shorter than its predecessor, but also makes it (to my eyes) visually crowded and more tiring to read. Second, Apress was not prompt about changing the project title after their decision to target Python 2, with the result that Amazon had “Python 3” in the title all the way through the book's release in late December — resulting in 1-star reviews for the first few weeks!

I will, by the way, soon be blogging about my experience rewriting the book's example code to work under Python 3. This should help two groups of people: anyone porting a Python 2 network application to Python 3 will probably run into the same issues I did; and people who are already experimenting with Python 3 will want to know how to apply the book's techniques to the newer version of the language.

Feel free to submit questions about the book or its example programs to the Bitbucket source code repository where the code is stored. The revision took far more effort than I had expected, but I am very happy to have found a format into which I could distill so much of my own — and the Python community's — network programming experience, so that it will help programmers for years to come. Enjoy!