You are not logged in Log in Join
You are here: Home » Members » Chui Tey » How to set up and test xmlrpc in Zope

Log in
Name

Password

 

How to set up and test xmlrpc in Zope

Main idea

Zope is a not only a web server, but also an xmlrpc server. This means in addition to getting a html rendered zope object, you can execute object.method calls on the server and get the results returned in xml.

What follows is a more detailed elaboration of Eric Kidd's example of xmlrpc.

Step by step guide

  1. In your Zope directory, create a directory called Extensions. This is the where all your external python functions can be written

  2. In the Extensions directory, create a text file called hello.py with the following:
    def hello_world(name):
       return "Hello, " + name + "!"
    
  3. In your Zope manage screen, create a folder called test

  4. In the test folder create an External Method.

  5. Set the ID to pyhello, Function to hello_world and Module to hello

  6. Convince yourself that the external method works:
    http://localhost:8080/test/pyhello?name=Chui+Tey

  7. Now pretend you are on another machine far, far, away.
    Run python

  8. Include path to yourzopedir/lib/python, and import the xmlrpc library

    import sys
    sys.append('C:\\progra~1\\teyzope\\lib\\python')
    import xmlrpclib
    
  9. Now try the following

    s=xmlrpclib.Server("http://localhost:8080")
    print s.Zope.test.pyhello("Chui Tey")
    

  10. You can also do this

    s=xmlrpclib.Server("http://localhost:8080/Zope/test/")
    print s.pyhello("Chui Tey")
    

    s=xmlrpclib.Server("http://localhost:8080")
    print s.test.pyhello("Chui Tey")
    #Zope object is no longer required to be referenced in versions 2.2 onwards

    #s.Zope.test.pyhello("Chui Tey")
    

  11. Or just for fun try

    s=xmlrpclib.Server("http://localhost:8080/Zope/")
    print s.index_html()
    In which case the server returns the index_html as a string

  12. For even more fun, change hello.py to
    def hello_world(name):
        from time import time, localtime, asctime
        return asctime(localtime(time())) ,"Hello, " + name + "!" 
    
    and try the calling the routines again.

Passing arguments to dtml methods

New

  1. In the root folder create this dtml method
    Id: dtmlHello
    Hello there <dtml-var username>
  2. Test this on your web browser:
    http://localhost:8080/dtmlHello?username=Chui+Tey
  3. Now in python
  4. s=xmlrpclib.Server("http://localhost:8080/Zope/")
    mapping = {'username':'Chui Tey'}
    or
    mapping = {}
    mapping['username']='Chui Tey'
    print s.dtmlHello(mapping)

Links

  1. Must read if you are calling ZClasses through XMLRPC. XML RPC Woes

What else

You can also directly implement an xmlrpc server by subclassing the xmlrpcserver.py, available for download from Pythonware