Version 3 (modified by brendan.mcadams@…, 8 years ago) (diff)

There are 2 ds in 'added', not 3...

NOTE: All credit for this code goes to Crast in

This uses SimpleXMLRPCDispatcher which is part of the standard Python lib in 2.4 (And possibly earlier versions).

In discussing ways of handling XML-RPC for Django, I realised I really needed a way to do it without patching Django's code. Crast in #django came up with a great solution, which I have modified and tweaked a bit.

I've included it here. Feel free to fiddle with it and make it your own ... All this code is post-mr

Any crappy & garbage code is completely mine; I'm still learning Python so bear with me. The hacks I added for self-documentation output are just that; any improvements to them would probably be a good thing.

First, setup your to map an XML-RPC service:

urlpatterns = patterns('',
    # XML-RPC
     (r'^xml_rpc_srv/', 'yourproject.yourapp.xmlrpc.rpc_handler'),

Then, in the appropriate place, create a file called

# Patchless XMLRPC Service for Django
# Kind of hacky, and stolen from Crast on
# it tells you about itself and it's methods
# Brendan W. McAdams <>

# SimpleXMLRPCDispatcher lets us register xml-rpc calls w/o

from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
from django.http import HttpResponse

dispatcher = SimpleXMLRPCDispatcher()

def rpc_handler(request):
	the actual handler:
	if you setup your properly, all calls to the xml-rpc service
	should be routed through here.

	response = HttpResponse()
	if len(request.POST):
		methods = dispatcher.system_listMethods()

		for method in methods:
			# right now, my version of SimpleXMLRPCDispatcher always
			# returns "signatures not supported"... :(
			sig = dispatcher.system_methodSignature(method)

			# this just reads your docblock, so fill it in!
			help =  dispatcher.system_methodHelp(method)



	return response

def multiply(a, b):
	Multiplication is fun!
	Takes two arguments, which are multiplied together.
	Returns the result of the multiplication!
	return a*b

# the dispatcher then maps the args down.
dispatcher.register_function(multiply, 'multiply')

That's it!


Here's a quick and dirty client example for testing:

import sys
import xmlrpclib
result = rpc_srv.multiply(sys.argv[1], sys.argv[2])

Based on experience, I do recommend that you use Dictionaries for your args rather than long args, but I think that's personal preference (It allows named arguments and eliminates 'out of order' argument issues).

Have fun!

  • Brendan W. McAdams