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