[Welcome] [TitleIndex] [WordIndex

Problem

You want to provide an interface into your application that can be used by other programs. For example, a bug tracking system could support some way for users to write scripts that retrieve the list of bugs assigned to them.

Solution

Write an XML-RPC interface. Quixote includes a helper function in the quixote.util module that simplifies the task.

from quixote.util import xmlrpc

def rpc (request, rpc_process):
    return xmlrpc(request, rpc_process)

def rpc_process (meth, params):
    if meth == 'list_bugs':
        user, password = params
        return retrieve_bugs_for_user(user, password)
    else:
        raise RuntimeError, "Unknown XML-RPC method: %r" % meth

To call the interface implemented above from a Python script, you would use the following code:

import xmlrpclib
s = xmlrpclib.Server('http://your.server.example.com/rpc')
bugs = s.list_bugs('amk', 'password')

XML-RPC implementations are available for most other languages, so the list_bugs() method could be called from any of them.

Discussion

XML-RPC's data types are relatively limited. You can return numbers, strings, lists, and dictionaries. You can't return Python objects, complex numbers,or Python's None value (the most irritating omission).

Consult the web-services.txt documentation file included with Quixote for more information about implementing XML-RPC interfaces.

The 'RPC' stands for Remote Procedure Call, since you're basically providing a way to call a function on your server. REST is a competing application design approach that uses URLs to represent operations and queries, and therefore has certain nice properties. For example, if you have a URL representing "amk's bug list", this URL can be bookmarked, forwarded to other people, and have RDF assertions made about it, none of which are possible with XML-RPC queries.


CategoryCookbook CategoryQuixote1


2010-09-22 22:14