[Twisted-Python] call XMLRPC

Phil Mayers p.mayers at imperial.ac.uk
Tue Apr 17 02:46:09 MDT 2012


On 04/17/2012 03:38 AM, joel tremblet wrote:

> I try to declare the Proxy(OF_XMLRPC_URL) differently, without success
>
> Where is my mistake ....

That code is all wrong, and (to be blunt) pretty horrible too.

Why have you re-implemented a HTTP protocol yourself? You're bound to 
get this wrong (and in fact have, in several ways). Don't do this. Use 
the HTTP stack that comes with Twisted.

Anyway, your major mistake in the 1st example is that, at the end of the 
lineReceived method, you always call the sendResponse() method. This 
happens immediately, before the XMLRPC reply has come back, so your code 
sends "no value". You need to attach the sendResponse method to the 
deferred returned from the xmlrpc call.

I would junk it and start again, like this:

from twisted.web import server, resource, xmlrpc, http

proxy = xmlrpc.Proxy(XMLRPC_URL)

class MyResource(resource.Resource):
   isLeaf = True
   def render_GET(self, request):
     sessid = request.args['sessid'][0]
     keyid = request.args['keyid'][0]
     d = proxy.callRemote('checkCommand', sessid, keyid)
     d.addCallback(self._done, request)
     d.addErrback(self._err, request)
     return server.NOT_DONE_YET

   def _done(self, res, request):
     if res!=1:
       request.setResponseCode(406)
     else:
       request.setResponseCode(200)
     request.finish()

   def _err(self, failure, request):
     request.setResponseCode(405)
     request.finish()

reactor.listenTCP(8080, server.Site(MyResource()))
reactor.run()




More information about the Twisted-Python mailing list