Foundations of Python Network Programming

(WallPaper) #1

Chapter 5 ■ Network Data aND Network errors


92


In such cases, you will add exception handlers around specific sequences of network operations that you want
to treat as having succeeded or failed as a single combined operation. “If anything goes wrong in here, then I will just
give up, wait ten minutes, and then start the attempt to send that e-mail all over again.” Here the structure and logic
of the network operations that you are performing—and not user or programmer convenience—will guide where you
deploy try...except clauses.


Summary


For machine information to be placed on the network, it has to be transformed so that regardless of whatever
private and idiosyncratic storage mechanism is used inside your machine, the data is presented using a public
and reproducible representation that can be read on other systems, by other programs, and perhaps even by other
programming languages.
For text, the big question will be choosing an encoding so that the symbols you want to transmit can be changed
into bytes, since 8-bit octets are the common currency of an IP network. Binary data will require your attention to
make sure that bytes are ordered in such a way that is compatible between different machines; the Python struct
module will help you with this. Finally, data structures and documents are sometimes best sent using something like
JSON or XML, which provides a common way to share structured data between machines.
When using TCP/IP streams, a big question you will face is about framing: how, in the long stream of data, will
you tell where a particular message starts and ends? There are many possible techniques for accomplishing this,
all of which must be handled with care since recv() might return only part of an incoming transmission with each
call. Special delimiter characters or patterns, fixed-length messages, and chunked-encoding schemes are all possible
ways to festoon blocks of data so that they can be distinguished.
Not only will Python pickles transform data structures into strings that you can send across the network, but also
the pickle module will know where an incoming pickle ends. This lets you use pickles not only to encode data but
also to frame the individual messages on a stream. The zlib compression module, which is often used with HTTP,
also can tell when a compressed segment comes to an end and thus can provide you with inexpensive framing.
Sockets can raise several kinds of exceptions, as can network protocols that your code uses. The choice of when
to use try...except clauses will depend on your audience—are you writing a library for other developers or a tool for
end users? It will also depend on semantics: you can wrap a whole section of your program in a try...except if all of
that code is doing one big thing from the point of view of the caller or end user.
Finally, you will want to wrap operations separately with a try...except that can be automatically retried in case
the error is transient and the call might be able to succeed later.

Free download pdf