XML-RPC over Zope 3, a quick tutorial
What 's XML-RPC ?
XML-RPC is a very simple Remote Procedure Control encoded in xml that can be used to interact with an application server from another program (another server, a rich client, or some Ajax'ed web Page)
More infos at wikipedia
XML-RPC in Zope 3
Zope 3 comes with a very handy approach to provide XML-RPC features. You just have to write a view class that provides methods that will be available as RPC methods.
If you are coming for Zope 2 world, please read up philliKon's slides at worldcookery (other Z3 goodies can be found there).
My First XMLRPC Package
Let's write a small Zope 3 package to provide an XML-RPC method over all server folders, that provides a folder listing.
This product is a folder composed of two files:
- xmlprctest.py: contains the xmlrpc view class that implements the method.
- configure.zcml: contains the configuration that will be read by Zope when it starts.
- __init__.py: an empty file that makes the folder beeing a Package.
FolderListing View class in xmlrpctest.py
Writing a XML-RPC view is extremely simple
from zope.app.publisher.xmlrpc import XMLRPCView
class FolderListing(XMLRPCView):
def contents(self):
""" returns the folder content """
return list(self.context.keys())
The contents() method just returns the context elements.
Introspection
Zope 3 will also automatically provide three extra methods for XML-RPC Introspection, that helps on API discovering from the client:
- listMethods(): Lists all xmlrpc methods (ie views) registered for the current object
- methodHelp(method_name): Returns the method documentation of the given method.
- methodSignature(method_name): Returns the method documentation of the given method.
Get more info about introspection here: introspection.
Since Python does not provide static typing, methodSignature needs a bit of help to return the signature of the method.
A method decorator called xmlrpccallable is provided and can be used on the View:
from zope.app.publisher.xmlrpc import XMLRPCView
from zope.app.xmlrpcintrospection.xmlrpcintrospection import xmlrpccallable
class FolderListing(XMLRPCView):
@xmlrpccallable(str)
def contents(self):
""" returns the folder content """
return list(self.context.keys())
The first decorator parameter is the return type, and the next parameters are the arguments type, if needeed.
Linking the view in configure.zcml
In the configuration file, we will tell Zope 3 that FolderListing() has to be instanciated and used everytime a XML-RPC call is made on a zope folder:
The xmlrpc:view directive, provided by zope.app.publisher.xmlrpc hooks everything up.
Setting up the package
The three files are saved into a folder called xlmrpctest into {$INSTANCE}/lib/python.
Last but not least, a zcml file, called xmlrpctest-configure.zcml,has to be added in {$INSTANCE}/etc/package-includes/ to let zope 3 know about the package.
xmlrpctest-configure.zcml:
<include package="xmlrpctest" />That's all, Zope can be restarted !
Trying out the XML-RPC method
Let's try out our product, with a few lines of python, using xmlrpclib:
from XMLRPCAuth import BasicAuthTransport
from xmlrpclib import ServerProxy
transport = BasicAuthTransport('admin', 'admin')
proxy = ServerProxy('http://localhost:8080/ok', transport=transport)
print 'method list:'
for method in proxy.listMethods():
signature = proxy.methodSignature(method)[0]
returned_type = signature[0]
arguments_type = ', '.join(signature[1:])
print ' + %s(%s) -> %s: %s' % (method, arguments_type, returned_type,
proxy.methodHelp(method).strip())
This small program will display all XML-RPC methods available on the folder ok, with their signatures.
The XMLRPCAuth package is a simple module that provides a basic authorization transport, and can be taken here: XMLRPCAuth
tziade@Tarek:/home/tziade$ python z3test.py method list: + contents() -> str: returns the content of the folder
(Post originally written by Tarek Ziadé on the old Nuxeo blogs.)
Subscribe to Feed
Follow us on Twitter

