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