[Twisted-Python] SOAP, XML-RPC-services and webpages in one Resource-object

Andrew Dalke dalke at dalkescientific.com
Wed Jun 11 16:56:25 EDT 2003


Thomas Weholt:
> I got a root-resource object which handles generation of content for an
> entire site. I need a server which serves xml-rpc *and* soap 
> webservices on
> the same port as the root-resource. How can I accomplish this? I've 
> allready
> subclassed the root-resource from twisted.web.resource. Can I use any 
> sort
> of mixin to do this?

You can do it as a mixin, like this

class MyXMLRPC(xmlrpc.XMLRPC):
   def xmlrpc_spam(self, i):
     return ", ".join(["spam"]*i)

class MySOAP(soap.SOAPPublisher):
   def soap_hello(self, name):
     return "Hello, %s" % (name,)

class YourRootResource(..., MyXMLRPC, MySOAP):
     def render(self, request):
       ct = request.getHeader("Content-Type"):
       if ct == "text/xml":
         # Either XML-RPC or SOAP
         if request.getHeader("SOAPAction") is None:
           return MyXMLRPC.render(self, request)
         return MySOAP.render(self, request)
       # proceed as normal

This works because both SOAP and XML-RPC send a "text/xml" in the
content-type, and SOAP also sends a SOAPAction in the header.

However, I would do it with composition, and not as a mixin.  That is,

class YourRootResource(Request):
     def __init__(self):
       self.xmlrpc_handler = MyXMLRPC()
       self.soap_handler = MySOAP()
     def render(self, request):
       ct = request.getHeader("Content-Type"):
       if ct == "text/xml":
         # Either XML-RPC or SOAP
         if request.getHeader("SOAPAction") is None:
           return self.xmlrpc_handler.render(self, request)
         return self.soap_heandler.render(self, request)
       # proceed as normal

This is because both the SOAP and XML-RPC implements make reference to
other methods in the class defintion, like "_gotResult", and I would
rather not have the possibility of reusing one of those methods by
accident.

					Andrew
					dalke at dalkescientific.com





More information about the Twisted-Python mailing list