I l@ve RuBoard Previous Section Next Section

13.2 Making an XML-RPC Method Call

Credit: Rael Dornfest, Jeremy Hylton

13.2.1 Problem

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

13.2.2 Solution

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:

# needs Python 2.2 or xmlrpclib from http://www.pythonware.com/products/xmlrpc/
from xmlrpclib import Server

server = Server("http://www.oreillynet.com/meerkat/xml-rpc/server.php")

print server.meerkat.getItems(
    {'search': '[Pp]ython', 'num_items': 5, 'descriptions': 0}
)

13.2.3 Discussion

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/.

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.

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!

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

from xmlrpclib import Server
server = Server("http://www.oreillynet.com/meerkat/xml-rpc/server.php")

class MeerkatQuery:
    def _ _init_ _(self, search, num_items=5, descriptions=0):
        self.search = search
        self.num_items = num_items
        self.descriptions = descriptions

q = MeerkatQuery("[Pp]ython")
print server.meerkat.getItems(q)

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.

13.2.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/.

    I l@ve RuBoard Previous Section Next Section