Changes between Version 18 and Version 19 of XML-RPC

06/01/2007 03:17:28 PM (8 years ago)

fix vandalism



    v18 v19  
    2 == '''''1923turk grup deuksX>>''''' ==
     1= XML-RPC =
     3NOTE: All credit for this code goes to Crast in
     5This uses SimpleXMLRPCDispatcher which is part of the standard Python lib in 2.4 (And possibly earlier versions).
     8In 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.
     10I've included it here.  Feel free to fiddle with it and make it your own ... All this code is '''post-mr'''
     12Any 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.
     14First, setup your to map an XML-RPC service:
     19urlpatterns = patterns('',
     20    # XML-RPC
     21     (r'^xml_rpc_srv/', 'yourproject.yourapp.xmlrpc.rpc_handler'),
     26Then, in the appropriate place, create a file called
     31# Patchless XMLRPC Service for Django
     32# Kind of hacky, and stolen from Crast on
     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
     36# Brendan W. McAdams <>
     38# SimpleXMLRPCDispatcher lets us register xml-rpc calls w/o
     39# running a full XMLRPC Server.  It's up to us to dispatch data
     41from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
     42from django.http import HttpResponse
     44# Create a Dispatcher; this handles the calls and translates info to function maps
     45#dispatcher = SimpleXMLRPCDispatcher() # Python 2.4
     46dispatcher = SimpleXMLRPCDispatcher(allow_none=False, encoding=None) # Python 2.5
     50def rpc_handler(request):
     51        """
     52        the actual handler:
     53        if you setup your properly, all calls to the xml-rpc service
     54        should be routed through here.
     55        If post data is defined, it assumes it's XML-RPC and tries to process as such
     56        Empty post assumes you're viewing from a browser and tells you about the service.
     57        """
     59        response = HttpResponse()
     60        if len(request.POST):
     61                response.write(dispatcher._marshaled_dispatch(request.raw_post_data))
     62        else:
     63                response.write("<b>This is an XML-RPC Service.</b><br>")
     64                response.write("You need to invoke it using an XML-RPC Client!<br>")
     65                response.write("The following methods are available:<ul>")
     66                methods = dispatcher.system_listMethods()
     68                for method in methods:
     69                        # right now, my version of SimpleXMLRPCDispatcher always
     70                        # returns "signatures not supported"... :(
     71                        # but, in an ideal world it will tell users what args are expected
     72                        sig = dispatcher.system_methodSignature(method)
     74                        # this just reads your docblock, so fill it in!
     75                        help =  dispatcher.system_methodHelp(method)
     77                        response.write("<li><b>%s</b>: [%s] %s" % (method, sig, help))
     79                response.write("</ul>")
     80                response.write('<a href=""> <img src="" border="0" alt="Made with Django." title="Made with Django."></a>')
     82        response['Content-length'] = str(len(response.content))
     83        return response
     85def multiply(a, b):
     86        """
     87        Multiplication is fun!
     88        Takes two arguments, which are multiplied together.
     89        Returns the result of the multiplication!
     90        """
     91        return a*b
     93# 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...
     96dispatcher.register_function(multiply, 'multiply')
     99That's it!
     101You can pretty much write a standard python function in there, just be sure to register it with the dispatcher when you're done. 
     103Here's a quick and dirty client example for testing:
     107import sys
     108import xmlrpclib
     109rpc_srv = xmlrpclib.ServerProxy("http://localhost:8000/xml_rpc_srv/")
     110result = rpc_srv.multiply( int(sys.argv[1]), int(sys.argv[2]))
     111print "%d * %d = %d" % (sys.argv[1], sys.argv[2], result)
     114Based 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).
     116Have fun!
     118- [  Brendan W. McAdams <>]
     122I wrote up [ a modified version of the XML-RPC view] that uses a template for documentation. -- [ Adam Blinkinsop <>]
     126I've taken the basics of the SimpleXMLRPCDispatcher above and have turned it into a distributable Django app, [ django_xmlrpc]. -- [ Graham Binns]
Back to Top