Serving XML-RPC Requests

Credit: Brian Quinlan

Problem

You need to implement an XML-RPC server.

Solution

The xmlrpclib package also makes writing XML-RPC servers pretty easy. Here’s how you can write an XML-RPC server:

# server coder sxr_server.py
# needs Python 2.2 or the XML-RPC package from PythonWare

import SimpleXMLRPCServer

class StringFunctions:
    def _ _init_ _(self):
        # Make all of the Python string functions available through
        # python_string.func_name
        import string
        self.python_string = string

    def _privateFunction(self):
        # This function cannot be called directly through XML-RPC because
        # it starts with an underscore character '_', i.e., it's "private"
        pass

    def chop_in_half(self, astr):
        return astr[:len(astr)/2]

    def repeat(self, astr, times):
        return astr * times

if _ _name_ _=='_ _main_ _':
    server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8000))
    server.register_instance(StringFunctions(  ))
    server.register_function(lambda astr: '_' + astr, '_string')
    server.serve_forever(  )

And here is a client that accesses the server you just wrote:

# server coder sxr_client.py
# needs Python 2.2 or the XML-RPC package from PythonWare

import xmlrpclib

server = xmlrpclib.Server('http://localhost:8000')
print server.chop_in_half('I am a confidant guy')
print server.repeat('Repetition is the key to learning!\n', 5)
print server._string('<= underscore')
print server.python_string.join(['I', 'like it!'], " don't ")
print server._privateFunction(  )    # will throw an exception

Discussion

This recipe demonstrates the creation of a simple XML-RPC server using the SimpleXMLRPCServer class. It requires Python 2.2 or later or the XML-RPC package from PythonWare (http://www.pythonware.com/products/xmlrpc/index.htm).

SimpleXMLRPCServer is a simple class that listens for HTTP requests on a specified port and dispatches any XML-RPC calls to a registered instance or a registered function. This recipe demonstrates both usages. To create a server, we instantiate SimpleXMLRPCServer, supplying the hostname and port for the server. Then, on that instance, we can call register_instance as many times as needed to make other instances available as services. Alternately, we can call register_function to make functions similarly available as services. Once we have registered all the instances and functions we want to expose, we call serve_forever on the server instance, and our XML-RPC server is active. Yes, it is really that simple.

Registering a function (as opposed to an instance) is necessary if your function’s name begins with an underscore (_) or contains characters not allowed in Python identifiers (e.g., Unicode characters, plus signs, etc.) Note that dotted names (e.g., python_string.join) are correctly resolved for registered instances.

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

Get Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.