Legend:

NOTE: All credit for this code goes to Crast in irc.freenode.net:#django...

4

5

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

6

7

8

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.

9

10

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

11

12

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.

13

14

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

15

16

17

{{{

18

#!python

19

urlpatterns = patterns('',

20

# XML-RPC

21

(r'^xml_rpc_srv/', 'yourproject.yourapp.xmlrpc.rpc_handler'),

22

)

23

}}}

24

25

26

Then, in the appropriate place, create a file called xmlrpc.py

27

28

29

{{{

30

#!python

31

# Patchless XMLRPC Service for Django

32

# Kind of hacky, and stolen from Crast on irc.freenode.net:#django

33

# Self documents as well, so if you call it from outside of an XML-RPC Client

34

# it tells you about itself and its methods

35

#

36

# Brendan W. McAdams <brendan.mcadams@thewintergrp.com>

37

38

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

39

# running a full XMLRPC Server. It's up to us to dispatch data

40

41

from SimpleXMLRPCServer import SimpleXMLRPCDispatcher

42

from django.http import HttpResponse

43

44

# Create a Dispatcher; this handles the calls and translates info to function maps

# you have to manually register all functions that are xml-rpc-able with the dispatcher

94

# the dispatcher then maps the args down.

95

# The first argument is the actual method, the second is what to call it from the XML-RPC side...

96

dispatcher.register_function(multiply, 'multiply')

97

}}}

98

99

That's it!

100

101

You can pretty much write a standard python function in there, just be sure to register it with the dispatcher when you're done.

102

103

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

104

105

{{{

106

#!python

107

import sys

108

import xmlrpclib

109

rpc_srv = xmlrpclib.ServerProxy("http://localhost:8000/xml_rpc_srv/")

110

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

111

print "%d * %d = %d" % (sys.argv[1], sys.argv[2], result)

112

}}}

113

114

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, eliminates 'out of order' argument issues and it makes the code more self-documenting).

I wrote up [http://www.personal-api.com/train/2007/feb/01/pingbacks-xml-rpc-and-django/ a modified version of the XML-RPC view] that uses a template for documentation. -- [mailto:hackerblinks+django@gmail.com Adam Blinkinsop <hackerblinks+django@gmail.com>]

123

124

----

125

126

I've taken the basics of the SimpleXMLRPCDispatcher above and have turned it into a distributable Django app, [http://code.google.com/p/django-xmlrpc django_xmlrpc]. -- [mailto:graham.binns+django-xmlrpc@gmail.com Graham Binns]