Linuxdoc Linux Questions
Click here to ask our community of linux experts!
Custom Search

7. Using XML-RPC with Python

Fredrik Lundh has provided an excellent XML-RPC library for Python.

To install, download the latest version. You can either stick the *.py files in the same directory as your Python code, or you can install them in your system's Python directory.

RedHat 6.2 users can type the following:

bash$ mkdir xmlrpclib-0.9.8
bash$ cd xmlrpclib-0.9.8
bash$ unzip ../xmlrpc-0.9.8-990621.zip
bash$ python
python> import xmlrpclib
python> import xmlrpcserver
python> Control-D
bash$ su -c 'cp *.py *.pyc /usr/lib/python1.5/'

We import two of the *.py files to trick Python into compiling them. Users of other platforms should consult their Python documentation.

For more Python examples, see the article XML-RPC: It Works Both Ways on the O'Reilly Network.

7.1. A Python Client

The following program shows how to call an XML-RPC server from Python:

import xmlrpclib

# Create an object to represent our server.
server_url = 'http://xmlrpc-c.sourceforge.net/api/sample.php';
server = xmlrpclib.Server(server_url);

# Call the server and get our result.
result = server.sample.sumAndDifference(5, 3)
print "Sum:", result['sum']
print "Difference:", result['difference']