How to set up and test xmlrpc in Zope
Created by .
Last modified on 2003/08/05.
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
- In your Zope directory, create a directory called Extensions. This is the where all your external python functions can be written
- In the Extensions directory, create a text file called hello.py with the following:
def hello_world(name):
return "Hello, " + name + "!"
- In your Zope manage screen, create a folder called test
- In the test folder create an External Method.
- Set the ID to pyhello, Function to hello_world and Module to hello
- Convince yourself that the external method works:
http://localhost:8080/test/pyhello?name=Chui+Tey
- Now pretend you are on another machine far, far, away.
Run python
- Include path to yourzopedir/lib/python, and import the xmlrpc library
import sys
sys.append('C:\\progra~1\\teyzope\\lib\\python')
import xmlrpclib
- Now try the following
s=xmlrpclib.Server("http://localhost:8080")
print s.Zope.test.pyhello("Chui Tey")
- 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")
- 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
- 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
- In the root folder create this dtml method
Id: dtmlHello
Hello there <dtml-var username>
- Test this on your web browser:
http://localhost:8080/dtmlHello?username=Chui+Tey
- Now in python
s=xmlrpclib.Server("http://localhost:8080/Zope/")
mapping = {'username':'Chui Tey'}
or
mapping = {}
mapping['username']='Chui Tey'
print s.dtmlHello(mapping)
Links
- 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
|