文章来自《Python cookbook》.

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

::-- huangyi [2006-04-22 18:48:25]

1. 描述

1.1. 问题 Problem

你需要向一个 XML-RPC 服务器发起一个方法调用

You need to make a method call to an XML-RPC server.

1.2. 解决 Solution

xmlrpclib 包使得编写 XML-RPC 客户端非常容易。比如, 我们可以使用 XML-RPC 来访问 O'Reilly's Meerkat 服务并获取五条最新的关于 Python 有关的信息:

The xmlrpclib package makes writing XML-RPC clients very easy. For example, we can use XML-RPC to access O'Reilly's Meerkat server and get the five most recent items about Python:

   1 # needs Python 2.2 or xmlrpclib from http://www.pythonware.com/products/xmlrpc/
   2 from xmlrpclib import Server
   3 
   4 server = Server("http://www.oreillynet.com/meerkat/xml-rpc/server.php")
   5 
   6 print server.meerkat.getItems(
   7     {'search': '[Pp]ython', 'num_items': 5, 'descriptions': 0}
   8 )

1.3. 讨论 Discussion

XML-RPC 是一种简单的轻量的进行分布式处理的方式。 xmlrpclib使得编写 XML-RPC 客户端和服务器都变得非常简单, 从 Python 2.2 开始他就被加到标准库里来了, 不过如果你使用老版本Python的话,你仍然可以从 http://www.pythonware.com/products/xmlrpc/ 得到它。

XML-RPC is a simple and lightweight approach to distributed processing. xmlrpclib, which makes it easy to write XML-RPC clients and servers in Python, has been part of the core Python library since Python 2.2, but you can also get it for older releases of Python from http://www.pythonware.com/products/xmlrpc/.

使用 xmlrpclib , 首先通过传递一个你想要连接的 URL 来实例化一个服务器的代理 (ServerProxy类, 为了保持向后兼容也叫做 Server 类)。然后你就可以通过这个实例上访问任何远程 XML-RPC 服务器提供的任何方法。在本例中, 你知道 Meerkat 提供一个 getItems的方法,所以如果你在服务器-代理实例上调用同名方法, 此实例将该调用转接到服务器并返回结果。

To use xmlrpclib, instantiate a proxy to the server (the ServerProxy class, also known as the Server class for backward compatibility) by passing in the URL to which you want to connect. Then, on that instance, access whatever methods the remote XML-RPC server supplies. In this case, you know that Meerkat supplies a getItems method, so if you call the method of the same name on the server-proxy instance, the instance relays the call to the server and returns the results.

这个访案使用到了 O'Reilly's Meerkat 服务, 该服务本来是给 诸如新闻、产品公告等内容的企业联合会用的。特别地, 这个方案向 Meerkat 查询五条和 "Python" 或 "python" 相关的最新条目。 如果你想试一下的化, 我得先警告你, 取决于你的网络连接、当天时间、和网络繁忙程序, 其响应时间的没有人知道的。如果脚本化了很长时间才响应, 并不意味着你做错了什么, 而是说你得耐心一点!

This recipe uses O'Reilly's Meerkat service, intended for the syndication of contents such as news and product announcements. Specifically, the recipe queries Meerkat for the five most recent items mentioning either "Python" or "python". If you try this, be warned that, depending on the quality of your Internet connection, the time of day, and the level of traffic on the Internet, response times from Meerkat are variable. If the script takes a long time to answer, it doesn't mean you did something wrong, it just means you have to be patient!

通过传递原始的字典来使用 xmlrpclib 也是不错的选择, 只是有点不那么 Pythonic。 这里是一个简单的替代方案, 看起来这个似乎要好一些。

Using xmlrpclib by passing raw dictionaries is quite workable, but rather unPythonic. Here's an easy alternative that looks quite a bit nicer:

   1 from xmlrpclib import Server
   2 server = Server("http://www.oreillynet.com/meerkat/xml-rpc/server.php")
   3 
   4 class MeerkatQuery:
   5     def _ _init_ _(self, search, num_items=5, descriptions=0):
   6         self.search = search
   7         self.num_items = num_items
   8         self.descriptions = descriptions
   9 
  10 q = MeerkatQuery("[Pp]ython")
  11 print server.meerkat.getItems(q)

当然, 你可以有好几种方式来包装实例的属性和他们的默认值, 不过这种变化的关键在于, 作为传递给 getItems 方法的参数, 一个拥有正确属性的对象实例, 和一个拥有相同信息作为其条目的字典对象效果是相同的。

Of course, you can package the instance attributes and their default values in several different ways, but the point of this variation is that, as the argument to the getItems method, an instance object with the right attributes works just as well as a dictionary object with the same information packaged as dictionary items.

1.4. 参考 See Also

The XML-RPC library ships with recent versions of Python; if it isn't in your version of Python, you can get it from http://www.pythonware.com/products/xmlrpc/; Meerkat is at http://www.oreillynet.com/meerkat/.