1 | | = XML-RPC = |
2 | | |
3 | | |
4 | | NOTE: All credit for this code goes to Crast in irc.freenode.net:#django... |
5 | | |
6 | | This uses SimpleXMLRPCDispatcher which is part of the standard Python lib in 2.4 (And possibly earlier versions). |
7 | | |
8 | | |
9 | | 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. |
10 | | |
11 | | I've included it here. Feel free to fiddle with it and make it your own ... All this code is '''post-mr''' |
12 | | |
13 | | 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. |
14 | | |
15 | | First, setup your urls.py to map an XML-RPC service: |
16 | | |
17 | | |
18 | | {{{ |
19 | | #!python |
20 | | urlpatterns = patterns('', |
21 | | # XML-RPC |
22 | | (r'^xml_rpc_srv/', 'yourproject.yourapp.xmlrpc.rpc_handler'), |
23 | | ) |
24 | | }}} |
25 | | |
26 | | |
27 | | Then, in the appropriate place, create a file called xmlrpc.py |
28 | | |
29 | | |
30 | | {{{ |
31 | | #!python |
32 | | # Patchless XMLRPC Service for Django |
33 | | # Kind of hacky, and stolen from Crast on irc.freenode.net:#django |
34 | | # Self documents as well, so if you call it from outside of an XML-RPC Client |
35 | | # it tells you about itself and its methods |
36 | | # |
37 | | # Brendan W. McAdams <brendan.mcadams@thewintergrp.com> |
38 | | |
39 | | # SimpleXMLRPCDispatcher lets us register xml-rpc calls w/o |
40 | | # running a full XMLRPC Server. It's up to us to dispatch data |
41 | | |
42 | | from SimpleXMLRPCServer import SimpleXMLRPCDispatcher |
43 | | from django.http import HttpResponse |
44 | | |
45 | | # Create a Dispatcher; this handles the calls and translates info to function maps |
46 | | #dispatcher = SimpleXMLRPCDispatcher() # Python 2.4 |
47 | | dispatcher = SimpleXMLRPCDispatcher(allow_none=False, encoding=None) # Python 2.5 |
48 | | |
49 | | |
50 | | |
51 | | def rpc_handler(request): |
52 | | """ |
53 | | the actual handler: |
54 | | if you setup your urls.py properly, all calls to the xml-rpc service |
55 | | should be routed through here. |
56 | | If post data is defined, it assumes it's XML-RPC and tries to process as such |
57 | | Empty post assumes you're viewing from a browser and tells you about the service. |
58 | | """ |
59 | | |
60 | | response = HttpResponse() |
61 | | if len(request.POST): |
62 | | response.write(dispatcher._marshaled_dispatch(request.raw_post_data)) |
63 | | else: |
64 | | response.write("<b>This is an XML-RPC Service.</b><br>") |
65 | | response.write("You need to invoke it using an XML-RPC Client!<br>") |
66 | | response.write("The following methods are available:<ul>") |
67 | | methods = dispatcher.system_listMethods() |
68 | | |
69 | | for method in methods: |
70 | | # right now, my version of SimpleXMLRPCDispatcher always |
71 | | # returns "signatures not supported"... :( |
72 | | # but, in an ideal world it will tell users what args are expected |
73 | | sig = dispatcher.system_methodSignature(method) |
74 | | |
75 | | # this just reads your docblock, so fill it in! |
76 | | help = dispatcher.system_methodHelp(method) |
77 | | |
78 | | response.write("<li><b>%s</b>: [%s] %s" % (method, sig, help)) |
79 | | |
80 | | response.write("</ul>") |
81 | | response.write('<a href="http://www.djangoproject.com/"> <img src="http://media.djangoproject.com/img/badges/djangomade124x25_grey.gif" border="0" alt="Made with Django." title="Made with Django."></a>') |
82 | | |
83 | | response['Content-length'] = str(len(response.content)) |
84 | | return response |
85 | | |
86 | | def multiply(a, b): |
87 | | """ |
88 | | Multiplication is fun! |
89 | | Takes two arguments, which are multiplied together. |
90 | | Returns the result of the multiplication! |
91 | | """ |
92 | | return a*b |
93 | | |
94 | | # you have to manually register all functions that are xml-rpc-able with the dispatcher |
95 | | # the dispatcher then maps the args down. |
96 | | # The first argument is the actual method, the second is what to call it from the XML-RPC side... |
97 | | dispatcher.register_function(multiply, 'multiply') |
98 | | }}} |
99 | | |
100 | | That's it! |
101 | | |
102 | | You can pretty much write a standard python function in there, just be sure to register it with the dispatcher when you're done. |
103 | | |
104 | | Here's a quick and dirty client example for testing: |
105 | | |
106 | | {{{ |
107 | | #!python |
108 | | import sys |
109 | | import xmlrpclib |
110 | | rpc_srv = xmlrpclib.ServerProxy("http://localhost:8000/xml_rpc_srv/") |
111 | | result = rpc_srv.multiply( int(sys.argv[1]), int(sys.argv[2])) |
112 | | print "%d * %d = %d" % (sys.argv[1], sys.argv[2], result) |
113 | | }}} |
114 | | |
115 | | 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). |
116 | | |
117 | | Have fun! |
118 | | |
119 | | - [mailto:bwmcadams@NOSPAM.gmail.com Brendan W. McAdams <bwmcadams@NOSPAM.gmail.com>] |
120 | | |
121 | | ---- |
122 | | |
123 | | 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>] |
124 | | |
125 | | ---- |
126 | | |
127 | | 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] |
| 1 | steve%252Bmadden%252Bshoes%252B%252B%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Findex.html%252B%25255Dsteve%252Bmadden%252Bshoes%25255B%25252FURL%25255D%252B%252B%252B%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Findex.html%252B%25253Esteve%252Bmadden%252Bshoes%25253C%25252Fa%25253E%252B%252B%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Findex.html%252Bsteve%252Bmadden%252Bshoes%252B%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dman%25252Dshoes.html%252B%25253Esteve%252Bmadden%252Bman%252Bshoes%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dshoes%25252Dstore.html%252B%25253Esteve%252Bmadden%252Bshoes%252Bstore%25253C%25252Fa%25253E%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dshoes%25252Dstore.html%252B%25255Dsteve%252Bmadden%252Bshoes%252Bstore%25255B%25252FURL%25255D%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dwomens%25252Drabena%25252Dshoes.html%252B%25253Esteve%252Bmadden%252Bwomens%252Brabena%252Bshoes%25253C%25252Fa%25253E%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dwoman%25252Dshoes.html%252B%25255Dsteve%252Bmadden%252Bwoman%252Bshoes%25255B%25252FURL%25255D%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Findex.html%252B%25255Dsteve%252Bmadden%252Bshoes%25255B%25252FURL%25255D%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dman%25252Dshoes.html%252B%25255Dsteve%252Bmadden%252Bman%252Bshoes%25255B%25252FURL%25255D%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dwomens%25252Dshoes.html%252B%25255Dsteve%252Bmadden%252Bwomens%252Bshoes%25255B%25252FURL%25255D%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dshoes%25252Doutlet.html%252B%25253Esteve%252Bmadden%252Bshoes%252Boutlet%25253C%25252Fa%25253E%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fcheap%25252Dsteve%25252Dmadden%25252Dshoes.html%252B%25255Dcheap%252Bsteve%252Bmadden%252Bshoes%25255B%25252FURL%25255D%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fdiscount%25252Dsteve%25252Dmadden%25252Dshoes.html%252B%25253Ediscount%252Bsteve%252Bmadden%252Bshoes%25253C%25252Fa%25253E%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dwomens%25252Drabena%25252Dshoes.html%252B%25255Dsteve%252Bmadden%252Bwomens%252Brabena%252Bshoes%25255B%25252FURL%25255D%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dwomens%25252Dshoes.html%252B%25253Esteve%252Bmadden%252Bwomens%252Bshoes%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fcheap%25252Dsteve%25252Dmadden%25252Dshoes.html%252B%25253Echeap%252Bsteve%252Bmadden%252Bshoes%25253C%25252Fa%25253E%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dshoes%25252Doutlet.html%252B%25255Dsteve%252Bmadden%252Bshoes%252Boutlet%25255B%25252FURL%25255D%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fdiscount%25252Dsteve%25252Dmadden%25252Dshoes.html%252B%25255Ddiscount%252Bsteve%252Bmadden%252Bshoes%25255B%25252FURL%25255D%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dleopard%25252Dshoes.html%252B%25253Esteve%252Bmadden%252Bleopard%252Bshoes%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dwoman%25252Dshoes.html%252B%25253Esteve%252Bmadden%252Bwoman%252Bshoes%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Findex.html%252B%25253Esteve%252Bmadden%252Bshoes%25253C%25252Fa%25253E%25250D%25250A%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeocities.com%25252Fshoeshere%25252Fsteve%25252Dmadden%25252Dshoes%25252Fsteve%25252Dmadden%25252Dleopard%25252Dshoes.html%252B%25255Dsteve%252Bmadden%252Bleopard%252Bshoes%25255B%25252FURL%25255D%25250D%25250A |