Recipe15.1.Making an XML-RPC Method Call


Recipe 15.1. Making an XML-RPC Method Call

Credit: Rael Dornfest, Jeremy Hylton

Problem

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

Solution

The xmlrpclib module 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:

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} )

Discussion

XML-RPC is a simple, lightweight approach to distributed processing. xmlrpclib, which makes it easy to write XML-RPC clients in Python, is part of the Python Standard Library.

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

This recipe uses O'Reilly's Meerkat service, intended for 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 recipe, be warned that response times from Meerkat are variable, depending on the quality of your Internet connection, the time of day, and the level of traffic on the Internet. If the script takes a long time to answer, it doesn't mean you did something wrongit just means you have to be patient!

Using xmlrpclib by passing raw dictionaries, as in this recipe's code, is quite workable but somewhat unPythonic. Here's an easy alternative that looks nicer:

from xmlrpclib import Server server = Server("http://www.oreillynet.com/meerkat/xml-rpc/server.php") class MeerkatQuery(object):     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)

You can package the instance attributes and their default values in several different ways, but the main point of this variant 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.

See Also

The xmlrpclib module is part of the Python Standard Library and is well documented in its chapter of the Library Reference portion of Python's online documentation. Meerkat is at http://www.oreillynet.com/meerkat/.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2004
Pages: 420

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net