The idea of this patch is to add support to Django for basic XML-RPC capabilities. It works mostly like URL patterns in that there is a ROOT_RPCCONF setting that points to a module that defines a rpc call registry like this:
rpccalls = {
'anton': 'gallery.test.anton',
}
So the registry is a simple dictionary that maps method names (they can be structured with "." - for example blogger.post) to functions in modules (function "anton" in module gallery.test in this case).
Additionally you need to add a mapping for some URL to the rpc view:
urlpatterns = patterns('',
(r'^RPC/', 'django.core.rpc.call'),
)
The last part is the django.core.rpc module:
import sys
import time
import xmlrpclib
from django.utils.httpwrappers import HttpResponse
from django.conf import settings
dispatch = {}
def call(request):
"""
This is the view you need to map into your URL space to process RPC
calls.
"""
p, u = xmlrpclib.getparser()
p.feed(request.raw_post_data)
p.close()
args = u.close()
method = u.getmethodname()
func = dispatch.get(method, None)
if func is not None:
result = func(*args)
xml = xmlrpclib.dumps((result,), methodresponse=1)
else:
xml = xmlrpclib.dumps(xmlrpclib.Fault(-32601, 'method unknown: %s' % method), methodresponse=1)
return HttpResponse(xml, mimetype='text/xml; charset=utf-8')
# build the dispatch table for rpc calls out of ROOT_RPCCONF
for (fn, fc) in __import__(settings.ROOT_RPCCONF, '', '', ['']).rpccalls.items():
p = fc.rfind('.')
modname = fc[:p]
funcname = fc[p+1:]
dispatch[fn] = getattr(__import__(modname, '', '', ['']), funcname)
After setting up all this (django.core.rpc should go into the django source - that's the actual patch), you could use your new RPC call like this:
import xmlrpclib
srv = xmlrpclib.Server('http://your.server.here/RPC/')
print srv.anton(5,6)
print srv.anton('anton','berta')
print srv.anton([1,2,3,4],[5,6,7,8})
I think this would allow people for much easier integration of simple XML-RPC webservices into django while keeping the framework idea of django intact.