文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

::-- huangyi [2006-04-22 17:06:31]

1. 简介 Introduction

Credit: Jeremy Hylton, PythonLabs

本章的方案描述了一些在分布式系统中使用 Python 的简单技术。编写分布式系统是很困难的, 而且没有一种方法能一劳永逸的解决所有问题。本章的解决访案的目的就是帮助你获得一些让 机器之间相互交流 的代码,然后你就可以开始编写应用程序了。

The recipes in this chapter describe some simple techniques for using Python in distributed systems. Programming distributed systems is hard, and recipes alone won't even come close to solving all your problems. What the recipes do is help you get programs on different computers talking to each other so you can start writing applications.

远程过程调用 (RPC) 是一种非常有吸引力的构建分布式系统的方法。网络传输的细节通过一个貌似平常的过程调用的接口暴露出来。当你调用一个位于远程服务器上的函数时, RPC系统负责所有的传输细节。它将参数编码, 这样他们就能通过网络传输到服务器上去, 而服务器也可以使用不同内部形式来表达这些数据。它然后在远程服务器上调用合适的函数并等待其响应。

Remote procedure call (RPC) is an attractive approach to structuring a distributed system. The details of network communication are exposed through an interface that looks like normal procedure calls. When you call a function on a remote server, the RPC system is responsible for all the communication details. It encodes the arguments so they can be passed over the network to the server, which might use different internal representations for the data. It invokes the right function on the remote machine and waits for a response.

本章的方案使用到三个不同的提供RPC接口的系统: CORBA, SOAP, 和 XML-RPC。这些系统都是很吸引人的, 因为通过他们可以很容易的将程序连接在一起, 不管他们运行在不同的计算机上还是用不同的语言编写。

The recipes here use three different systems that provide RPC interfaces: CORBA, SOAP, and XML-RPC. These systems are attractive because they make it easy to connect programs, whether they are running on different computers or are written in different languages.

在2.2 release以后的python版本中 你都可以在标准库中找到 Fredrik Lundh 的 XML-RPC 库。对于更早的 Python, 对于 CORBA 和 SOAP, 你还需要赶状一些其他的软件, 才能开始本章的学习。这些解决方案包含需要哪些软件的提示。

You can find Fredrik Lundh's XML-RPC library for Python in the standard library, starting with the 2.2 release. For earlier versions of Python, and for CORBA and SOAP, you'll need to install more software before you can get started. The recipes include pointers to the software you need.

Python标准库同样提供了一个用来处理底层网络编程的优秀的模块集合: socket, select, asyncore, 和 asynchat。 它也包含了用来组织数据并通过 sockets发送的模块: struct, pickle, 和 xdrlib。这些模块依次为许多其他模块提供了plumbing。Jeff Bauer提供了一个使用 telnetlib 模块来给远程机器发送命令的方案。

The Python standard library also provides a good set of modules for doing the lower-level work of network programming: socket, select, asyncore, and asynchat. It also has modules for marshaling data and sending it across sockets: struct, pickle, and xdrlib. These modules, in turn, provide the plumbing for many other modules. Jeff Bauer offers a recipe using the telnetlib module to send commands to remote machines.

有四个方案是集中在 XML-RPC 上的, XML-RPC 是一种使用 XML 和 HTTP 在程序之间交换简单数据结构的新协议。 Rael Dornfest示范了如何编写一个 XML-RPC 客户端程序, 这个程序从 O'Reilly's Meerkat 服务上获取数据。这是一个包括 import 都只有三行的方案, 这是它最大的卖点。

Four of the recipes focus on XML-RPC, a new protocol that uses XML and HTTP to exchange simple data structures between programs. Rael Dornfest demonstrates how to write an XML-RPC client program that retrieves data from O'Reilly's Meerkat service. It's a three-line recipe, including the import statement, which is its chief appeal.

Brian Quinlan 和 Jeff Bauer 贡献了两个用于构建 XML-RPC 服务器的方案。 Quinlan 在方案 13.3 展示了如何使用 Python 2.2 标准库中的 SimpleXMLRPCServer 模块处理外来请求。Bauer's 方案 13.4 用到了Medusa, 一个用来编写异步网络服务器的框架。 在这两个方案里面, 库都做了大部分的工作,而不是仅仅几行初始化和注册的代码, 服务器看起来就象通常的 Python 代码。

Brian Quinlan and Jeff Bauer contribute two recipes for constructing XML-RPC servers. Quinlan shows how to use the SimpleXMLRPCServer module from the Python 2.2 standard library to handle incoming requests in Recipe 13.3. Bauer's Recipe 13.4 uses Medusa, a framework for writing asynchronous network servers. In both cases, the libraries do most of the work. Other than a few lines of initialization and registration, the server looks like normal Python code.

SOAP 和 XML-RPC 一样, 也是一个基于 XML 的协议。Graham Dumpleton 在方案13.5中解释了如何创建一个可以和客户端以任何协议交谈的服务器, 此方案是三个使用了他的 OSE 框架的方案之一。 这两个协议是很简单的, 一个简单的 HTTP 服务器和服务实现就可以同时支持这两种协议。There are gotchas, of course。 Dumpleton 提到了几个。 For starters, XML-RPC 不支持 None, 而 SOAP 不支持空字典。

SOAP is an XML-based protocol that shares its origins with XML-RPC. Graham Dumpleton explains how to create a server that can talk to clients with either protocol in Recipe 13.5, one of three recipes that use his OSE framework. The two protocols are similar enough that a single HTTP server and service implementation can support both protocols. There are gotchas, of course. Dumpleton mentions several. For starters, XML-RPC does not support None, and SOAP does not support empty dictionaries.

CORBA 可以用来代替基于 XML 的协议, 他是一个基于对象的RPC机制, 他使用他自己的协议IIOP。 对比 XML-RPC 和 SOAP, CORBA是一个成熟的技术; 他在1991年就被提出。Python 语言绑定在2000年二月正式通过, 而且还有几个 ORBs (简单的说就是 CORBA 服务器) 支持 Python。Duncan Grisby 在方案13.6中展示了让一个 CORBA 客户端和服务器运行起来的需要的基本要素, 该方案用到 omniORB (一个免费的 ORB), 和他为它编写的 Python 绑定。

An alternative to the XML-based protocols is CORBA, an object-based RPC mechanism that uses its own protocol, IIOP. Compared to XML-RPC and SOAP, CORBA is a mature technology; it was introduced in 1991. The Python language binding was officially approved in February 2000, and several ORBs (roughly, CORBA servers) support Python. Duncan Grisby lays out the basics of getting a CORBA client and server running in Recipe 13.6, which uses omniORB, a free ORB, and the Python binding he wrote for it.

CORBA的复杂是出了名的了, 不过 Grisby 的方案让他看起来很简单。CORBA 客户端示例中涉及到的步骤比 XML-RPC 客户端示例要多, 不过他们并不难理解。要将一个 XML-RPC 客户端连上一个服务器,你只需要一个URL即可。而要将一个 CORBA 客户端和服务器相连, 你需要一个特定的 corbaloc URL, 而且你还需要知道服务器的接口。当然, 你需要知道这个协议无关的接口, 不过 CORBA 明确地用到它(?)。通常, CORBA 还提供一些其他的特性, 比如接口、类型检查、传递对象引用等等 (而且它还同时支持 None 和 空字典)。

CORBA has a reputation for complexity, but Grisby's recipe makes it look straightforward. There are more steps involved in the CORBA client example than in the XML-RPC client example, but they aren't hard to follow. To connect an XML-RPC client to a server, you just need a URL. To connect a CORBA client to a server, you need a special corbaloc URL, and you need to know the server's interface. Of course, you need to know the interface regardless of protocol, but CORBA uses it explicitly. Generally, CORBA offers more features梥uch as interfaces, type checking, passing references to objects, and more (and it supports both None and empty dictionaries).

不管你选择什么协议或系统, 这里的方案都可以帮助你入门。交互式的通讯编程是构建一个分布式系统的重要组成部分, 不过它也只是一部分而已。一旦你有了可以工作的客户端和服务器, 你会发现你还需要解决其他的一些有趣的、困难的问题, 比如错误检测, 并发处理, 和安全, to name a few。本章的解决方案不会处理这些问题, 不过他们可以让你不必陷入无关紧要的通讯协议细节中去。

Regardless of the protocols or systems you choose, the recipes here can help get you started. Interprogram communication is an important part of building a distributed system, but it's just one part. Once you have a client and server working, you'll find you have to deal with other interesting, hard problems, such as error detection, concurrency, and security, to name a few. The recipes here won't solve these problems, but they will prevent you from getting caught up in unimportant details of the communication protocols.

1.1. 讨论 Discussion

...

1.2. 参考 See Also