Menu

Using Zope as an XML-RPC Client

January 12, 2000

Amos Latteier

In addition to being an XML-RPC server Zope can act as an XML-RPC client. While Zope doesn't come with built-in XML-RPC client objects it does support Python scripting and includes the standard Python XML-RPC client library. Pretty much anything you can do in Python you can do in Zope.

Here's a step by step recipe for using Zope as an XML-RPC client.

Creating an External Method

To use Zope as an XML-RPC client, first create an External Method. An External Method is a Zope object which uses Python scripting. For more information about External Methods see the Zope Content Manager's Guide.

Here's the code for an External Method that calls a method running on Frontier using XML-RPC.

import xmlrpclib



def stateName(self, number):

    """

    Returns a state name given an integer between 1 and 50.

    """

    server=xmlrpclib.Server('http://betty.userland.com')

    return server.examples.getStateName(number)

Save this code to a file named 'xmlrpc_test.py' in the Zope top-level 'Extensions' directory. Choose External Method from the product add list. Specify 'stateName' as the Id, 'stateName' as the function name, 'xmlrpc_example' as the Python module file, and click 'Add'.

Calling the External Method

Now that you've created an External Method that uses XML-RPC you need to call it from Zope.

In general you will call your External Methods from other Zope objects such as DTML Documents. Here's an example of how to call the 'stateName' External Method. Create a DTML Document by selecting 'DTML Document' from the product add list. Give the document an Id of 'stateForm' and click 'Add and Edit'. Now enter this for the document content:


<dtml-var standard_html_header>



<p>

<form>

State Number (1-50): <input type="text" name="number:int">

<br>

<input type="submit" value="Find State Name">

</form>

</p>



<dtml-if number>

<h2>Results</h2>

<p>

<dtml-var number> : <dtml-var "stateName(number)">

</p>

</dtml-if>

 

<dtml-var standard_html_footer>

Now view the document to try it out. Type a number in the form and submit it.

The document displays a form that collects the state number and displays the results. The first paragraph displays the form. Notice that the form has no action, so this document will call itself. When you submit the form, the number variable will be set. Then the document's second paragraph will be displayed. The second paragraph calls the stateName External Method with the number variable as an argument.

Conclusion

You've seen first hand how to use Zope as an XML-RPC server and client. It's not hard to build a simple web application with Zope and XML-RPC.

Zope and XML-RPC provide a easy-to-use, scriptable distributed object system. Simplicity, reliance on Internet standards, and language independence make XML-RPC an appealing protocol. Zope's web object architecture make it an obvious choice for XML-RPC work.

Zope and XML-RPC provide a compelling example of how XML is allowing a shift from a world of web sites to a world of web applications.