| 1 | | info%252Bon%252Bfingertoe%252Bnails%252B%252B%25255BURL%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Finfo%25252Don%25252Dfingertoe%25252Dnails.html%252B%25255Dinfo%252Bon%252Bfingertoe%252Bnails%25255B%25252FURL%25255D%252B%252B%252B%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Finfo%25252Don%25252Dfingertoe%25252Dnails.html%252B%25253Einfo%252Bon%252Bfingertoe%252Bnails%25253C%25252Fa%25253E%252B%252B%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Finfo%25252Don%25252Dfingertoe%25252Dnails.html%252Binfo%252Bon%252Bfingertoe%252Bnails%252B%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fladies%25252Dgold%25252Drolex%25252Dwatchst%25252Dthomas.html%252B%25253Eladies%252Bgold%252Brolex%252Bwatchst%252Bthomas%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fauto%25252Dbody%25252Drepairsuction%25252Dcup.html%252B%25253Eauto%252Bbody%252Brepairsuction%252Bcup%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fheart%25252Dattack%25252Dsymptomswomen.html%252B%25253Eheart%252Battack%252Bsymptomswomen%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fmetastasized%25252Dprostate%25252Dcancerbone%25252Dpain.html%252B%25253Emetastasized%252Bprostate%252Bcancerbone%252Bpain%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fmacarenalos%25252Ddel%25252Drio.html%252B%25253Emacarenalos%252Bdel%252Brio%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fautomobile%25252Daccidentsimpact.html%252B%25253Eautomobile%252Baccidentsimpact%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fbehavior%25252Dmodificationcharts.html%252B%25253Ebehavior%252Bmodificationcharts%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Ffort%25252Deustisva.html%252B%25253Efort%252Beustisva%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fdreamhe%25252Dloves%25252Dyou%25252Dnot.html%252B%25253Edreamhe%252Bloves%252Byou%252Bnot%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fmixmatch%25252Dbodykit.html%252B%25253Emixmatch%252Bbodykit%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fclaspsjewelry%25252Dfindings.html%252B%25253Eclaspsjewelry%252Bfindings%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fbasketball%25252Drimpole.html%252B%25253Ebasketball%252Brimpole%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fmylodrop%25252Dthe%25252Dpressure.html%252B%25253Emylodrop%252Bthe%252Bpressure%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fjavascriptopen%25252Da%25252Dnew%25252Dwindow.html%252B%25253Ejavascriptopen%252Ba%252Bnew%252Bwindow%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fforex%25252Dbrokerdata.html%252B%25253Eforex%252Bbrokerdata%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Faccountingtax%25252Dsource%25252Dinc.html%252B%25253Eaccountingtax%252Bsource%252Binc%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fblockbuster%25252Dvideoscottsdale.html%252B%25253Eblockbuster%252Bvideoscottsdale%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fjeremypearl%25252Djam.html%252B%25253Ejeremypearl%252Bjam%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Ftargunet%25252Dinternet%25252Dwebpage%25252Dtranslator.html%252B%25253Etargunet%252Binternet%252Bwebpage%252Btranslator%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Ffamous%25252Dquotesmovie.html%252B%25253Efamous%252Bquotesmovie%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Ffloyd%25252Dcollegephotography%25252Dclasses.html%252B%25253Efloyd%252Bcollegephotography%252Bclasses%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fbetter%25252Dtogetherjack%25252Djohnson.html%252B%25253Ebetter%252Btogetherjack%252Bjohnson%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fjeep%25252Ddealerskansas.html%252B%25253Ejeep%252Bdealerskansas%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fpragueoklahoma.html%252B%25253Epragueoklahoma%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Foasisdont%25252Dlook%25252Dback%25252Din%25252Danger.html%252B%25253Eoasisdont%252Blook%252Bback%252Bin%252Banger%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Finfo%25252Don%25252Dfingertoe%25252Dnails.html%252B%25253Einfo%252Bon%252Bfingertoe%252Bnails%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fused%25252Dtruckskenworth.html%252B%25253Eused%252Btruckskenworth%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fclinical%25252Dnutritionlong%25252Dterm%25252Dcare.html%252B%25253Eclinical%252Bnutritionlong%252Bterm%252Bcare%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Fcalciumhigh%25252Dabsorption.html%252B%25253Ecalciumhigh%252Babsorption%25253C%25252Fa%25253E%25250D%25250A%25253Ca%252Bhref%25253D%252Bhttp%25253A%25252F%25252Fgeo.ya.com%25252Fshlc%25252Frange%25252Drover%25252Dsporttest.html%252B%25253Erange%252Brover%252Bsporttest%25253C%25252Fa%25253E%25250D%25250Ae |
| | 1 | NOTE: All credit for this code goes to Crast in irc.freenode.net:#django... |
| | 2 | |
| | 3 | This uses SimpleXMLRPCDispatcher which is part of the standard Python lib in 2.4 (And possibly earlier versions). |
| | 4 | |
| | 5 | |
| | 6 | 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. |
| | 7 | |
| | 8 | I've included it here. Feel free to fiddle with it and make it your own ... All this code is '''post-mr''' |
| | 9 | |
| | 10 | 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. |
| | 11 | |
| | 12 | First, setup your urls.py to map an XML-RPC service: |
| | 13 | |
| | 14 | |
| | 15 | {{{ |
| | 16 | #!python |
| | 17 | urlpatterns = patterns('', |
| | 18 | # XML-RPC |
| | 19 | (r'^xml_rpc_srv/', 'yourproject.yourapp.xmlrpc.rpc_handler'), |
| | 20 | ) |
| | 21 | }}} |
| | 22 | |
| | 23 | |
| | 24 | Then, in the appropriate place, create a file called xmlrpc.py |
| | 25 | |
| | 26 | |
| | 27 | {{{ |
| | 28 | #!python |
| | 29 | # Patchless XMLRPC Service for Django |
| | 30 | # Kind of hacky, and stolen from Crast on irc.freenode.net:#django |
| | 31 | # Self documents as well, so if you call it from outside of an XML-RPC Client |
| | 32 | # it tells you about itself and its methods |
| | 33 | # |
| | 34 | # Brendan W. McAdams <brendan.mcadams@thewintergrp.com> |
| | 35 | |
| | 36 | # SimpleXMLRPCDispatcher lets us register xml-rpc calls w/o |
| | 37 | # running a full XMLRPC Server. It's up to us to dispatch data |
| | 38 | |
| | 39 | from SimpleXMLRPCServer import SimpleXMLRPCDispatcher |
| | 40 | from django.http import HttpResponse |
| | 41 | |
| | 42 | # Create a Dispatcher; this handles the calls and translates info to function maps |
| | 43 | #dispatcher = SimpleXMLRPCDispatcher() # Python 2.4 |
| | 44 | dispatcher = SimpleXMLRPCDispatcher(allow_none=False, encoding=None) # Python 2.5 |
| | 45 | |
| | 46 | |
| | 47 | |
| | 48 | def rpc_handler(request): |
| | 49 | """ |
| | 50 | the actual handler: |
| | 51 | if you setup your urls.py properly, all calls to the xml-rpc service |
| | 52 | should be routed through here. |
| | 53 | If post data is defined, it assumes it's XML-RPC and tries to process as such |
| | 54 | Empty post assumes you're viewing from a browser and tells you about the service. |
| | 55 | """ |
| | 56 | |
| | 57 | response = HttpResponse() |
| | 58 | if len(request.POST): |
| | 59 | response.write(dispatcher._marshaled_dispatch(request.raw_post_data)) |
| | 60 | else: |
| | 61 | response.write("<b>This is an XML-RPC Service.</b><br>") |
| | 62 | response.write("You need to invoke it using an XML-RPC Client!<br>") |
| | 63 | response.write("The following methods are available:<ul>") |
| | 64 | methods = dispatcher.system_listMethods() |
| | 65 | |
| | 66 | for method in methods: |
| | 67 | # right now, my version of SimpleXMLRPCDispatcher always |
| | 68 | # returns "signatures not supported"... :( |
| | 69 | # but, in an ideal world it will tell users what args are expected |
| | 70 | sig = dispatcher.system_methodSignature(method) |
| | 71 | |
| | 72 | # this just reads your docblock, so fill it in! |
| | 73 | help = dispatcher.system_methodHelp(method) |
| | 74 | |
| | 75 | response.write("<li><b>%s</b>: [%s] %s" % (method, sig, help)) |
| | 76 | |
| | 77 | response.write("</ul>") |
| | 78 | 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>') |
| | 79 | |
| | 80 | response['Content-length'] = str(len(response.content)) |
| | 81 | return response |
| | 82 | |
| | 83 | def multiply(a, b): |
| | 84 | """ |
| | 85 | Multiplication is fun! |
| | 86 | Takes two arguments, which are multiplied together. |
| | 87 | Returns the result of the multiplication! |
| | 88 | """ |
| | 89 | return a*b |
| | 90 | |
| | 91 | # you have to manually register all functions that are xml-rpc-able with the dispatcher |
| | 92 | # the dispatcher then maps the args down. |
| | 93 | # The first argument is the actual method, the second is what to call it from the XML-RPC side... |
| | 94 | dispatcher.register_function(multiply, 'multiply') |
| | 95 | }}} |
| | 96 | |
| | 97 | That's it! |
| | 98 | |
| | 99 | You can pretty much write a standard python function in there, just be sure to register it with the dispatcher when you're done. |
| | 100 | |
| | 101 | Here's a quick and dirty client example for testing: |
| | 102 | |
| | 103 | {{{ |
| | 104 | #!python |
| | 105 | import sys |
| | 106 | import xmlrpclib |
| | 107 | rpc_srv = xmlrpclib.ServerProxy("http://localhost:8000/xml_rpc_srv/") |
| | 108 | result = rpc_srv.multiply( int(sys.argv[1]), int(sys.argv[2])) |
| | 109 | print "%d * %d = %d" % (sys.argv[1], sys.argv[2], result) |
| | 110 | }}} |
| | 111 | |
| | 112 | 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). |
| | 113 | |
| | 114 | Have fun! |
| | 115 | |
| | 116 | - [mailto:bwmcadams@NOSPAM.gmail.com Brendan W. McAdams <bwmcadams@NOSPAM.gmail.com>] |
| | 117 | |
| | 118 | ---- |
| | 119 | |
| | 120 | 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>] |
| | 121 | |
| | 122 | ---- |
| | 123 | |
| | 124 | 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] |
| | 125 | |