Foundations of Python Network Programming

Foundations of Python Network Programming is now available for purchase (Amazon). This book is designed to show you everything from fundamentals of networking and low-level protocol design to work with higher-level protocols such as IMAP, HTTP, and FTP. For more information, please see the letter to the reader from the book’s back cover.

This is not a basic reference like Python comes with. Rather, it’s a hands-on guide. There are over 6600 lines of example code and the text strives to show you the big picture. For instance, there are several different ways of getting directory information from an FTP server, and some are not documented for use this way. The chapter on FTP explains them and provides example code to illustrate. There’s also a blog page for your comments.


There have been several reviews of this book. Here are some links:

  • Slashdot has a review giving the book a 9 out of 10. One quote: “This may be the easiest review I’ve ever written. If you program in Python and you want to write Internet applications, go buy Foundations of Python Network Programming."

  • On Amazon, the book has a user rating of 4.5 out of 5. The editorial review mentions that “This book features extensive examples that not only demonstrate important concepts and practices, but also provide a cadre of fully-functional standalone programs." The editorial review, as well as user reviews, are both available on the book’s Amazon page.

Table of Contents

Below is the book’s Table of Contents along with a partial list of the examples you’ll find in each chapter:

  • Part I: Low-Level Networking

    • Chapter 1: Introduction to Client/Server Networking

      Provides basic concepts of TCP/IP networking for those new to it. Examples include very basic versions of: Gopher client, URL downloading program.

    • Chapter 2: Network Clients

      Constructing basic network clients: setting up sockets, connecting to servers, sending/receiving data, handling errors. Covers TCP and UDP. Examples include: connecting to servers, error handling, obtaining the exact current time from a UDP network time server.

    • Chapter 3: Network Servers

      Constructing basic network servers: binding to a port, listening, handling connections, logging, socket options, inetd servers. Handling multiple simultaneous connections is covered in Part VI. Examples include: TCP and UDP echo servers, TCP echo client, TCP and UDP inetd servers.

    • Chapter 4: DNS

      Advanced DNS topics: reverse lookups, advanced forward lookups, information about the local system. Examples include various DNS query and lookup utilities.

    • Chapter 5: Advanced Network Operations

      Advanced network topics that are usually applicable to both client and servers: half-open sockets, timeouts, network byte order, broadcasts, IPv6. Examples include: UDP broadcast sending and receiving programs, binding to specific interfaces, IPv6 HTTP client, network byte order example, cross-protocol DNS lookups.

  • Part II: Web Services

    • Chapter 6: HTTP

      Obtaining documents from a Web server. Examples include: obtaining information from headers, submitting GET and POST forms, handling errors, and dumping pages.

    • Chapter 7: HTML

      Parsing HTML documents. Examples include: basic parsing, obtaining the weather forecast for a region.

    • Chapter 8: XML and XML-RPC

      Handling XML documents and communicating with XML-RPC servers. Examples include: DOM tree parsing and mainpulation, XML-RPC news reader, interactive XML-RPC introspection client.

  • Part III: E-Mail

    • Chapter 9: Message Composition and Decoding

      Generating message headers and bodies. Using MIME to represent attachments, data encodings, header languages, etc. Examples include: date parsing, MIME decoding, attachments, multipart messages, header generation and parsing, MIME and non-MIME generation.

    • Chapter 10: SMTP

      Using the Simple Mail Transfer Protocol to transmit messages to remote servers. Covers topics such as establishing a connection, authentication, SSL/TLS, setting recipients, etc. Examples include: mail transmission program, TLS support.

    • Chapter 11: POP3

      Downloading mail using the simple POP3 protocol. Examples include: mailbox information, download messages, delete messages.

    • Chapter 12: IMAP

      A detailed look at the IMAP protocol, including basic connections, authentication methods, folder lists and selection, message searching, flag handling, message retrieval, message storage and copying, and UID handling. Presents the Twisted IMAP4Client library and a brief coverage of the built-in imaplib. Examples include: error handling, uploading messages, downloading messages, deleting messages, message structure, searching, more.

  • Part IV: General-Purpose Client Protocols

    • Chapter 13: FTP

      Using the popular protocol to transfer files, get directory listings, discover information about remote files, handle passive mode connects. Examples include: a basic recursive-download FTP client, directory parsing.

    • Chapter 14: Database Clients

      Using Python’s DB-API 2.0 to connect to database servers. Provides general DB-API background and specific examples using MySQL and PostgreSQL, and the zxJDBC connector. Examples include: connecting, querying, various ways of accessing the result set.

    • Chapter 15: SSL/TLS

      Provides a primer on the potential attacks network programs can face and ways to help reduce them by using SSL. Covers the standard socket.ssl in Python and the more powerful PyOpenSSL. Includes coverage and example code to implement key/certificate verification. Examples include: basic use of the libraries, verification.

  • Part V: Server-Side Frameworks

    • Chapter 16: SocketServer

      Covers this basic infrastructure for writing TCP servers. Includes coverage of BaseHTTPServer and CGIHTTPServer as well as the mix-in classes for forking and threading. Examples include: time server, IPv6 support, HTTP servers.

    • Chapter 17: SimpleXMLRPCServer

      Shows how to write your own XML-RPC server and interface it to new or existing code. Also covers derivitaves of SimpleXMLRPCServer. Examples include: math server, introspection support.

    • Chapter 18: CGI Library

      Explains how CGI fits in with a Web server, covering escaping, form processing, cookie handling, and document generation. Examples include: interactive quiz program, environment.

    • Chapter 19: mod_python

      This Apache module embeds a Python interpreter into the web server. It can be used not just to accelerate dynamic Web sites but also to customize the behavior of Apache itself. Examples include: interactive quiz program, escaping, versatile dispatching system.

  • Part VI: Multitasking

    • Chapter 20: Forking

      Covers the use of forking on Unix platforms under Python. Describes the mechanics of forking, different ways to handle dying child processes, error handling, locking, performance, and implications for server authors. Examples include: echo server, zombie process examples.

    • Chapter 21: Threading

      Covers the use of threading on multiple supported Python platforms. Describes the mechanics of threading, error handling, locking, performance, and implications for server authors. Examples include: echo server, semaphores, thread pools, multi-threaded client.

    • Chapter 22: Non-Blocking Sockets

      Covers the use of poll() to achieve the appearance of a multitasking server without using either forking or threading. It goes on to cover the higher-level Twisted libraries. Examples include: echo server, an inetd server, chat server.

Here is information on papers, documentation, and publications written by John Goerzen.

I am a programmer, manager, hobbyist, advocate, volunteer, dad, and nature lover. I live out on an old farmstead in rural Kansas that once belonged to my grandparents. The nearest paved road is about 3 miles away, and the nearest town (population 600) is 7 miles away. I have three incredible children, which I might occasionally mention on my blog.