Chapter 7. Network Protocols

Increasingly, people seem to misinterpret complexity as sophistication, which is baffling—the incomprehensible should cause suspicion rather than admiration. Possibly this trend results from a mistaken belief that using a somewhat mysterious device confers an aura of power on the user.

Niklaus Wirth

Overview

This chapter describes Python’s socket protocol support and the networking modules built on top of the socket module. These include client handlers for most popular Internet protocols, as well as several frameworks that can be used to implement Internet servers.

For the low-level examples in this chapter, I’ll use two protocols for illustration: the Internet Time Protocol and the Hypertext Transfer Protocol.

Internet Time Protocol

The Internet Time Protocol (RFC 868, Postel and Harrenstien, 1983) is a simple protocol that allows a network client to get the current time from a server.

Since this protocol is relatively lightweight, many (but far from all) Unix systems provide this service. It’s also about as easy to implement as a network protocol can possibly be. The server simply waits for a connection request and immediately returns the current time as a 4-byte integer containing the number of seconds since January 1, 1900.

In fact, the protocol is so simple that I can include the entire specification:

File: rfc868.txt Network Working Group J. Postel - ISI Request for Comments: 868 K. Harrenstien - SRI May 1983 Time Protocol This RFC specifies a ...

Get Python Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.