Calling XML-RPC Functions

The twisted.web.xmlrpc module includes client support for sending XML-RPC method calls to a server. Unlike the xmlrpclib module in the Python standard library, Twisted's XML-RPC client doesn't try to hide the fact that you're making a function call over the network (for one thing, method calls return a Deferred, so that your program can do other things while it's waiting for a response from the server). But it's still very easy to use.

5.4.1. How Do I Do That?

Create a twisted.web.xmlrpc.Proxy object pointing to the URI of your XML-RPC service. Use the callRemote method to call XML-RPC functions. Example 5-4 demonstrates a client that uses the XML-RPC services from Example 5-3.

Example 5-4. xmlrpc_client.py


from twisted.web import xmlrpc

from twisted.internet import reactor



class WikiTester(object):

 def _ _init_ _(self):

 self.wiki = xmlrpc.Proxy('http://localhost:8082/RPC2')



 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 XmlRpcTest..."

 pageData = "This is a test of XmlRpc"

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



 def getTestPage(self):

 print "Getting test page content..."

 return self.wiki.callRemote(

 'getRenderedPage', 'XmlRpcTest').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 xmlrpc_client.py, and it will run a few commands over XML-RPC:


 $ python xmlrpc_client.py

 Getting page list...

 Got page list: ['WikiHome', 'CurlTest', 'WgetTest', 'RestTest', 'PythonXmlRpcTest']

 Creating test page XmlRpcTest...

 Getting test page content...

 Got page content:

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

 Getting page list...

 Got page list: ['WikiHome', 'CurlTest', 'XmlRpcTest', 'WgetTest', 'RestTest',

 'PythonXmlRpcTest']

 

5.4.2. How Does That Work?

The callRemote method of xmlrpc.Proxy takes the name of the function to be called as the first argument, followed by an arbitrary number of additional arguments that will be passed to the function. When you use callRemote, the Proxy encodes your function call as XML, sends it to the server using an HTTP POST, and returns a Deferred. When the server responds, the Proxy decodes the XML 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

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