|
|
|
XML-RPC over Zope 3, a quick tutorial
Please read this blog entry from here
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 3Zope 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 PackageLet'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:
FolderListing View class in xmlrpctest.pyWriting 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. IntrospectionZope 3 will also automatically provide three extra methods for XML-RPC Introspection, that helps on API discovering from the client:
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.zcmlIn 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 packageThe 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 methodLet'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 Important announcement: Join the Nuxeo team and contribute to the Nuxeo project! We have open positions in France and the UK for open source Java EE developers and sales engineers, both junior and senior. Trackback PingsTrackback URL for this entry:
http://blogs.nuxeo.com/sections/blogs/tarek_ziade/2005_11_04_xml-rpc-over-zope-3/tbping
|
Nuxeo Bloggers: Log in! Search Nuxeo Blogs
About this blog
Tarek Ziadé Nuxeo Bloggers
Photos and Pictures
|
|
Nuxeo -
Indesko -
Nuxeo 5 Project
All content is copyrighted by their author. CPSSkins is Copyright © 2003-2006 by Jean-Marc Orliaguet. | CPS is Copyright © 2002-2006 by Nuxeo SAS. |