So far in this chapter, we’ve focused on the role of sockets in the classic client/server networking model. That’s one of their primary roles, but they have other common use cases as well.
In Chapter 5, for instance, we saw sockets as a basic IPC device between processes and threads on a single machine. And in Chapter 10’s exploration of linking non-GUI scripts to GUIs, we wrote a utility module (Example 10-23) which connected a caller’s standard output stream to a socket, on which a GUI could listen for output to be displayed. There, I promised that we’d flesh out that module with additional transfer modes once we had a chance to explore sockets in more depth. Now that we have, this section takes a brief detour from the world of remote network servers to tell the rest of this story.
Although some programs can be written or rewritten to converse
over sockets explicitly, this isn’t always an option; it may be too
expensive an effort for existing scripts, and might preclude desirable
nonsocket usage modes for others. In some cases, it’s better to allow
a script to use standard stream tools such as the
input built-in functions and
sys module file calls (e.g.,
sys.stdout.write), and connect them to
sockets only when needed.
Because such stream tools are designed to operate on text-mode files, though, probably the biggest trick here is fooling them into operating on the inherently binary mode and very different method interface of sockets. ...