xmlrpchandler has been abandoned. Please see instead BaseXMLRPCServer and XMLRPCServer at Allan Saddi's Python Modules.

Introduction

xmlrpchandler.py is an XML-RPC server for mod_python. It uses Fredrik Lundh's excellent xmlrpclib, which is included with Python.

Features:

License

xmlrpchandler.py is distributed under a BSD-style license.

Download

xmlrpchandler.py may be downloaded here. The current CVS revision is:
$Id: xmlrpchandler.py,v 1.10 2002/05/23 18:26:45 asaddi Exp $.

Usage

Create a directory to hold xmlrpchandler.py and your modules, for example, /path/to/xmlrpc-lib. Add something like the following to your httpd.conf or .htaccess.

.htaccess
<Files "xmlrpc">
  PythonPath "['/path/to/xmlrpc-lib']+sys.path"
  SetHandler python-program
  PythonHandler xmlrpchandler
  PythonDebug on
</Files>

And that's it! Function docstrings will be returned as help strings by system.methodHelp(). You may define a variable named __xmlrpc_signature within your functions to support system.methodSignature() (and to also enable argument checking). This variable must be one of the first variables defined (preferably right after the docstring) and it must be a constant string literal. For example:

__xmlrpc_signature = '[[retType, paramType1, paramType2, ...]]'

Where the types are INT, STRING, ARRAY, etc. (see the beginning of xmlrpchandler.py).

Module and function names that begin with an underscore ('_') will not be accessible by the XML-RPC interface.

Example

A sample server is running at http://www.flup.org/xmlrpc. Along with the system methods, test.sumprod(x, y) and test.sumAndDifference(x, y) are available.

example client calls using Python
>>> import xmlrpclib
>>> s = xmlrpclib.Server('http://www.flup.org/xmlrpc')
>>> s.test.sumprod(5, 7)
[12, 35]
>>> s.test.sumAndDifference(5, 7)
{'sum': 12, 'difference': -2}

And the source for test.py, which defines these two functions. test.py must be located in the same directory as xmlrpchandler.py in order to be recognized.

test.py
def sumprod(x, y):
    "Returns the sum and products of its arguments as an array."
    __xmlrpc_signature = '[[ARRAY, INT, INT]]'
    return x+y, x*y

def sumAndDifference(x, y):
    "Returns the sum and difference of its arguments as a struct."
    __xmlrpc_signature = '[[STRUCT, INT, INT]]'

    return { 'sum': x + y,
             'difference': x - y }

Copyright © 2002 Allan Saddi. All rights reserved.