Python in a Nutshell, Second Edition (In a Nutshell)
| There are many standards for distributed computing, from simple Remote Procedure Call (RPC) ones to rich object-oriented ones such as CORBA. You can find several third-party Python modules supporting these standards on the Internet. The Python standard library comes with support for both server and client use of a simple yet powerful standard known as XML-RPC. For in-depth coverage of XML-RPC, I recommend the book Programming Web Services with XML-RPC, by Simon St. Laurent and Joe Johnson (O'Reilly). XML-RPC uses HTTP as the underlying transport and encodes requests and replies in XML. For server-side support, see Section 19.2.2.4 in Chapter 19. Client-side support is supplied by module xmlrpclib. The xmlrcplib module supports a class ServerProxy, which you instantiate to connect to an XML-RPC server. An instance s of ServerProxy is a proxy for the server it connects to. In other words, you call arbitrary methods on s, and s packages up the method name and argument values as an XML-RPC request, sends the request to the XML-RPC server, receives the server's response, and unpackages the response as the method's result. The arguments to such method calls can be of any type supported by XML-RPC:
Module xmlrpclib supplies two factory functions.
Creates and returns an instance of Binary wrapping the given bytestring.
Creates and returns an instance of Boolean with the truth value of x. Module xmlrpclib supplies several classes.
x is a Python string of arbitrary bytes. b represents the same bytes as an XML-RPC binary object.
x is any Python value, and b has the same truth value as x.
x is a number of seconds since the epoch, as used in module time, covered in Chapter 12.
If the server at the given url supports introspection, s supplies an attribute s.server that in turn supplies three methods:
The following example uses xmlrpclib to access O'Reilly's Meerkat open wire service (see http://www.oreillynet.com/meerkat/ for more information about Meerkat) and displays the last few news items about Python. import xmlrpclib proxy = xmlrpclib.ServerProxy( 'http://www.oreillynet.com/meerkat/xml-rpc/server.php') results = proxy.meerkat.getItems({'search':'Python', 'num_items':7}) want_keys = 'title link description'.split( ) n = 0 for result in results: n = n + 1 for key in want_keys: print '%d. %s: %s' % (n, key.title( ), result.get(key)) print |