XML-RPC in Pythonby Dave Warner
The Internet, with its simple protocol and ubiquity, has opened up huge opportunities for programs to communicate between computers, a task that always seemed complex and daunting in the past. Now there is a new dilemma. Which framework should you use for automating communication?
The two most popular frameworks, Enterprise JavaBeans and Microsoft's COM, are highly expressive and robust, but also complex. They carry an enormous amount of processing overhead. Other requirements, such as cross-platform and cross-language capability, only add to the dilemma. The Simple Object Access Protocol (SOAP) has recently come to the forefront, promising to solve many of these problems. In active development, SOAP has several partial implementations available but is still a moving target. Some implementations cannot yet talk to each other.
So what's a developer to do?
What many developers have done is to use what is in place and what works now. And what works is XML-RPC. It's easy, quick, and consistent. XML-RPC fits a "glue" role that its big brother, SOAP, cannot. If you are looking for a large-scale framework for communicating between diverse servers and enterprise functionality like automated discovery of services, then XML-RPC will not fit. XML-RPC is, however, a quick, easy-to-use, reliable means of communicating between distributed locations, without worrying about what language the "other guy" has used. XML-RPC is a hybrid solution. It's not strictly object-oriented. The "RPC" portion of the acronym stands for Remote Procedure Call. RPC is very simple -- a user sends a request over the network to a specific location and receives a reponse. If this sounds suspiciously familar, it is -- the Web works this way. The death-knell for RPC was heard in the 1990s. Its developers were unable to standardize on the structure of data. Sun did try to implement a standard in XDR. It never quite caught on. With the rise of object-oriented programming, RPC fell into disfavor. By adding XML to the RPC portion, a standard, popular structure for data has been added to the process, breathing new life into RPC. So why use it?
The major, incontestable reason for using XML-RPC is that it works, and works well. With a very minimum of effort, a developer can install and use Python xmlrpclib, communicating with servers built in Python, PHP, Java, Perl, C++, and even COM. An entire chapter devoted to XML-RPC in the O'Reilly's best-selling Java and XML brought great attention to XML-RPC. In the Python world, the largest use of XML-RPC is via Zope. Zope has been able to send and respond to XML-RPC since mid-1999.
Enter PythonWare's xmlrpclib
Unlike other languages in its generation, Python has always had the ability to create persistent objects. It uses increasingly complex mechanisms to do so. These mechanisms range from the standard library package, "marshall," which converts simple objects for storage in flat files, to the "Zope Object Database," which rivals commercial Object-Oriented Databases (OODB) in its robustness. To "marshall" an object is to convert it into a different format. Within the context of xmlrpclib, marshalling a Python object means to convert it into an XML-RPC call. Unmarshalling is the reverse of that step.
Written by Fredrik Lundh of PythonWare, xmlrpclib hides all of the complexity in marshalling and unmarshalling objects. It goes beyond this to also hide the details of connecting to a server, sending a request, and receiving a response.
You can download the latest version of xmlrpclib.py (and get more information about the library) at the XML-RPC for Python page.
XML-RPC and Meerkat
Editor's note: Meerkat predated the popularity of syndication, feed services, and feed readers. Now that other groups are providing this service, we have removed Meerkat in favor of their better solutions. We maintain these articles for the sense of historical interest.
Rael Dornfest, a driving force in the adoption of XML in the PHP arena, has quite naturally constructed an XML-RPC interface to Meerkat, an open wire service that collects news information from many sources using RSS. Let's use this readily available server for our examples. First we'll import the pretty print (
pprint) module from the Python Standard Library. Some of the output from Meerkat is quite lengthy.
>>> from pprint import pprint
After importing xmlrpclib, connecting to the server using xmlrpclib consists of one line:
>>> meerkatsvr = xmlrpclib.Server("http://www.oreillynet.com/meerkat/xml-rpc/server.php")
Here we create an instance of an XML-RPC server that understands how to connect, send, and receive XML-RPC. Through recursive "sleight-of-hand," this server can even understand nested XML-RPC requests.
Next, let's request a list of the methods that are available from the server:
>>> pprint(meerkatsvr.system.listMethods()) ['meerkat.getChannels', 'meerkat.getCategories', 'meerkat.getChannelsByCategory', 'meerkat.getItems', 'system.listMethods', 'system.methodHelp', 'system.methodSignature']
You have just used XML-RPC to connect to a server across the Internet and retrieve a result set. Besides the simplicity of the approach, notice that the form of the result set returned by xmlrpclib is a native Python list.
Pages: 1, 2