Chapter 20. Sockets and Server-Side Network Protocol Modules
To communicate with the Internet, programs use objects known as sockets. The Python library supports sockets through module socket, as well as wrapping them into higher-level client-side modules, as covered in Chapter 19. To help you write server programs, the Python library also supplies higher-level modules to use as frameworks for socket servers. Standard and third-party Python modules and extensions also support asynchronous socket operations. This chapter covers socket, in "The socket Module" on page 521; server-side framework modules, in "The SocketServer Module" on page 528; asynchronous operation with standard Python library modules, in "Event-Driven Socket Programs" on page 533; and the bare essentials of the rich and powerful Twisted third-party package, in "The Twisted Framework" on page 539.
互联网通信程序通常使用套接字对象。和我们在19章提到的较高级客户端模块的包装一样， Python库通过socket模块提供套接字支持 。为了帮助我们编写服务器程序，Python库同样提供一些较高级的上层模块来作为套接字服务器的基本框架。官方模块、第三方Python模块和扩展模块还提供异步套接字操作。本章将在521页“套接字模块”中介绍套接字，在528页“套接字服务器模块”中介绍服务器端套接字框架模块，在533页“事件驱动型套接字程序”中介绍使用Python基本库的异步操作；并且在539页“Twisted框架”中从本质上介绍丰富而强大的Twisted第三方库。
The modules covered in this chapter offer many conveniences compared to C-level socket programming. However, in the end, the modules rely on native socket functionality supplied by the underlying operating system. While it is often possible to write effective network clients by using just the modules covered in Chapter 19 without really needing to understand sockets, writing effective network servers most often does require some understanding of sockets. Thus, the lower-level module socket is covered in this chapter and not in Chapter 19, even though both clients and servers use sockets.
However, I cover only the ways in which module socket lets your program access sockets; I do not try to impart a detailed understanding of sockets, TCP/IP, and other aspects of network behavior independent of Python that you may need to make use of socket's functionality. To understand socket behavior in detail on any kind of platform, I recommend W. Richard Stevens's Unix Network Programming, Volume 1 (Prentice Hall). Higher-level modules are simpler and more powerful, but a detailed understanding of the underlying technology is always useful, and sometimes it can prove indispensable.
- 但是，我只能介绍如何使用socket模块；我不会涉及到套接字的底层理解、TCP／IP以及其他使用Python的网络应用，这样你可能会从功能上了解套接字的使用。要了解底层套接字在各种平台上的详细信息，我推荐 W. Richard Stevens的Unix Network Programming 卷1 (Prentice Hall)。上层模块会更易用且强大，但是了解底层套接字技术总是有帮助的，并且有时是不可缺少的。