Calling SOAP Web Services

The twisted.web.soap module contains a soap client. You can use it to communicate with the SOAP Wiki service defined in Example 5-5.

5.7.1. How Do I Do That?

The twisted.web.soap.Proxy class provides the same callRemote method as twisted.web.xmlrpc.Proxy. Use callRemote to call a SOAP method and get a Deferred result. Example 5-6 demonstrates a SOAP client that communicates with the SOAP Wiki service from Example 5-5.

Example 5-6. soap_client.py


from twisted.web import soap

from twisted.internet import reactor



class WikiTester(object):

 def _ _init_ _(self):

 self.wiki = soap.Proxy('http://localhost:8082/SOAP')



 def runTests(self):

 self.listPages( ).addCallback(

 lambda _:self.createTestPage( )).addCallback(

 lambda _: self.getTestPage( )).addErrback(

 self._catchFailure).addCallback(

 lambda _: reactor.stop( ))



 def listPages(self):

 print "Getting page list..."

 return self.wiki.callRemote('listPages').addCallback(

 self._gotList)



 def _gotList(self, pages):

 print "Got page list:", pages



 def createTestPage(self):

 print "Creating test page SoapTest..."

 pageData = "This is a test of SoapRpc"

 return self.wiki.callRemote('setPage', 'SoapTest', pageData)



 def getTestPage(self):

 print "Getting test page content..."

 return self.wiki.callRemote(

 'getRenderedPage', 'SoapTest').addCallback(

 self._gotTestPage)



 def _gotTestPage(self, content):

 print "Got page content:"

 print content

 return self.listPages( )



 def _catchFailure(self, failure):

 print "Error:", failure.getErrorMessage( )



w = WikiTester( )

w.runTests( )

reactor.run( )

Run soap_client.py, and it will test calling some methods through SOAP:


 $ python soap_client.py

 Getting page list...

 Got page list: :

 ['WikiHome', 'CurlTest', 'XmlRpcTest', 'WgetTest', 'RestTest', 'PythonXmlRpcTest']

 Creating test page SoapTest...

 Getting test page content...

 Got page content:

 This is a test of <a href="SoapRpc">SoapRpc</a>

 Getting page list...

 Got page list: :

 ['WikiHome', 'CurlTest', 'SoapTest', 'XmlRpcTest', 'WgetTest', 'RestTest',

 'PythonXmlRpcTest']

 

5.7.2. How Does That Work?

The callRemote method of soap.Proxy takes the name of a function as the first argument, followed by any number of additional arguments that will be passed to the function. You can use keyword arguments instead, if the SOAP service you're using supports named arguments. When you use callRemote, the Proxy encodes your function call as a SOAP XML document, sends it to the server using an HTTP POST request, and returns a Deferred. When the server responds, the Proxy parses the result using SOAPpy and calls back the Deferred with the returned value.

Getting Started

Building Simple Clients and Servers

Web Clients

Web Servers

Web Services and RPC

Authentication

Mail Clients

Mail Servers

NNTP Clients and Servers

SSH

Services, Processes, and Logging



Twisted Network Programming Essentials
Twisted Network Programming Essentials
ISBN: 0596100329
EAN: 2147483647
Year: 2004
Pages: 107
Authors: Abe Fettig

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net