[Twisted-Python] XMLRPC Class in HTTP Server.

Jean-Paul Calderone exarkun at divmod.com
Mon Jul 9 10:14:07 EDT 2007

Wasn't going to reply to this, for reasons which will be obvious momentarily,
but I haven't seen a correct reply yet, so...

On Fri, 06 Jul 2007 09:46:38 +0200, Daniel de la Cuesta <daniel.cuesta at iavante.es> wrote:
><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
><body bgcolor="#ffffff" text="#000000">
>I have a twisted web server running using "twisted.web.http".<br>

You probably want to direct future questions on this topic to the
twisted-web mailing list, where you'll get more attention from
people more actively doing web things.

>The objective of the server is to convert files to another format.
>Currently I upload the files and the conversion params using an HTTP
>POST from a form.<br>
>Now I want to add XML-RPC support to the conversion process. It means
>the user could process the conversion using the http form or calling
>directly the XML-RPC class.<br>

HTTP POST is probably a better way to upload files, in general.  MIME-ish
encoding is a bit easier and has less overhead than base64 encoding.

>This is my pages dispatch:<br>
>class MyRequestHandler(http.Request):<br>
>&nbsp;&nbsp; <br>
>&nbsp;&nbsp;&nbsp; pages = { '/' : loginPage, <br>
>&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '/uploadHandler' : handleUpload,<br>
>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '/uploadform' : uploadForm,<br>
>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '/xmlrpc_converter': MyConverterXMLRPCClass() }<br>

Rarely is it correct to subclass Request.  You can probably just discard
all of the above code.  Aside from that, I don't know what the `pages'
attribute is.  I don't think anything pays any attention to it.

>What I want to do is offer the user two possibilities to upload the
>file, one of them using HTTP POST (calling to "uploadform") and the
>other using XML-RPC (caling to xmlrpc_converter).<br>
>I have tried to implement MyConverterXMLRPCClass() as follows:<br>
>&nbsp;&nbsp; <br>
>&nbsp;&nbsp;&nbsp; def __init(self):<br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """"""<br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass<br>
>&nbsp;&nbsp;&nbsp; def xmlrpc_mymethod1(self, param)<br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """My process""<br>

This part looks okay.

>But It down't work, when I try <a class="moz-txt-link-freetext" href="http://localhost:8000/xmlrpc_converter">http://localhost:8000/xmlrpc_converter</a> I
>get the following error:<br>
>"MyConverterXMLRPCClass instance has no __call__ method"<br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
>How can I call MyConverterXMLRPCClass from an url in my http server?<br>

I don't know how a MyConverterXMLRPCClass got called.  I suppose you have
some more code that you didn't include in this message which looks at the
pages dictionary on the request object and does something with them.
Whatever it is, I guess it isn't correct. ;)

Instead, what you should do is define a root resource which has
MyConverterXMLRPCClass as a child at the "xmlrpc_converter" segment.  You
can do this pretty easily using the basic Resource class in twisted.web:

    from twisted.web.resource import Resource
    from twisted.web.server import Site
    from twisted.internet import reactor

    root = Resource()
    root.putChild('xmlrpc_converter', MyConverterXMLRPCClass())
    site = Site(root)
    reactor.listenTCP(8080, site)

The Resource class is implemented to keep track of child resources added
to it with putChild so that when a request comes in for one of them, it
gives back the appropriate one and URL traversal or page rendering can

Hope this helps,


More information about the Twisted-Python mailing list