1 | import sys
|
---|
2 | import time
|
---|
3 | import xmlrpclib
|
---|
4 |
|
---|
5 | from django.utils.httpwrappers import HttpResponse
|
---|
6 | from django.conf import settings
|
---|
7 |
|
---|
8 | from django.core.rpcdispatch import dispatcher
|
---|
9 |
|
---|
10 | DEBUG = 0
|
---|
11 | if hasattr(settings, 'DEBUG_RPC'):
|
---|
12 | DEBUG = settings.DEBUG_RPC
|
---|
13 |
|
---|
14 | def call(request):
|
---|
15 | """
|
---|
16 | This is the view you need to map into your URL space to process RPC
|
---|
17 | calls.
|
---|
18 | """
|
---|
19 | p, u = xmlrpclib.getparser()
|
---|
20 | p.feed(request.raw_post_data)
|
---|
21 | p.close()
|
---|
22 | args = u.close()
|
---|
23 | method = u.getmethodname()
|
---|
24 | func = dispatcher().find(method)
|
---|
25 | if DEBUG: print method, func, args
|
---|
26 | if func is not None:
|
---|
27 | try:
|
---|
28 | result = func(*args)
|
---|
29 | xml = xmlrpclib.dumps((result,), methodresponse=1)
|
---|
30 | if DEBUG: print result
|
---|
31 | except Exception, e:
|
---|
32 | if DEBUG: print e
|
---|
33 | xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: %s' % e), methodresponse=1)
|
---|
34 | if DEBUG: print
|
---|
35 | else:
|
---|
36 | xml = xmlrpclib.dumps(xmlrpclib.Fault(-32601, 'method unknown: %s' % method), methodresponse=1)
|
---|
37 | return HttpResponse(xml, mimetype='text/xml; charset=utf-8')
|
---|
38 |
|
---|