Sharing Web Services with SOAP

Once you have SOAPpy installed, you can write a Twisted server that provides web services through SOAP.

5.6.1. How Do I Do That?

Because SOAP is based on HTTP, you enable SOAP web services by adding a special type of Resource to a twisted web server. Write a class that inherits from soap.SOAPPublisher and has methods starting with soap_. Make your class available as a web resource, as shown in Example 5-5, and you're ready to handle SOAP requests.

Example 5-5. soap_server.py

import wiki, rest_wiki from twisted.web import server, soap class WikiSoap(soap.SOAPPublisher): def _ _init_ _(self, wikiData): self.wikiData = wikiData def soap_hasPage(self, path): return self.wikiData.hasPage(path) def soap_listPages(self): return self.wikiData.listPages( ) def soap_getPage(self, path): return self.wikiData.getPage(path) def soap_getRenderedPage(self, path): return self.wikiData.getRenderedPage(path) def soap_setPage(self, path, data): self.wikiData.setPage(path, data) if __name__ == "_ _main_ _": import sys from twisted.internet import reactor datafile = sys.argv[1] wikiData = wiki.WikiData(datafile) siteRoot = rest_wiki.RootResource(wikiData) siteRoot.putChild('SOAP ', WikiSoap(wikiData)) reactor.listenTCP(8082, server.Site(siteRoot)) reactor.run( )

Run soap_server.py from the command line with the name of your Wiki datafile as an argument:

$ python soap_server.py wiki.dat

Once again, it will start up a web server on port 8082, but this time it will listen for SOAP requests at http://localhost:8082/SOAP. You should be able to test this using a SOAP library in any language; the next lab shows how to do so using a Twisted SOAP client.

5.6.2. How Does That Work?

The soap .SOAPPublisher class is the SOAP equivalent of the xmlrpc.XMLRPC class in Example 5-3. It's a subclass of Resource that parses the SOAP message in an incoming HTTP POST, runs the requested function, and then returns the SOAP-formatted result. Note that unlike XML-RPC, SOAP supports null values, so you don't have to avoid returning None from your methods.

Категории