Are you being served?
Setting up a ZSI server is simple enough. Listing 3 presents the same trivial
calendar SOAP server we used as an example for SOAP.py in the last column. (See Resources.)
Listing 3: ZSI calendar SOAP server
#!/usr/bin/env python
import sys, calendar
#Import the ZSI machinery
from ZSI import dispatch
def getMonth(year, month):
return calendar.month(year, month)
def getYear(year):
return calendar.calendar(year)
print "Starting server..."
dispatch.AsServer(port=8080)
|
Note that this is even easier than with SOAP.py. All you do is define
a function for each method. With the required parameters, variable number
and keyword arguments can be used as well for positional and named parameters.
The dispatch.AsServer()
call simply registers all the
defined functions as SOAP methods and starts up the HTTP server on the
specified port.
One problem is that ZSI's server code doesn't seem to have an easy way of
aligning the namespace used in the request. The documents claim there
is a dispatch.GetNS()
function that returns the namespace that
was used in the request element, but this doesn't seem to be the case.
This is a pretty serious omission as the namespace used in the request is an
essential part of that request.
The following ZSI client code exercises the calendar we just wrote:
Listing 4: ZSI calendar SOAP client
#http://xmethods.net/detail.html?id=175
import sys
#Import the ZSI client
from ZSI.client import Binding
u = ''
n = 'http://uche.ogbuji.net/eg/ws/simple-cal'
b = Binding(url=u, ns=n, host='localhost', port=8080)
result = b.getMonth(2002, 2)
print result[0]
result = b.getYear(2002)
print result[0]
|
To test it, just start the server using "$ python calendar-zsi.py" in one
console, then in another:
$ python2.1 cal-client.py
February 2002
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
2002
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 1 2 3
7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10
14 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17
21 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24
28 29 30 31 25 26 27 28 25 26 27 28 29 30 31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 5 1 2
8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9
15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16
22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23
29 30 27 28 29 30 31 24 25 26 27 28 29 30
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1
8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8
15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15
22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22
29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29
30
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 1
7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8
14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15
21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22
28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29
30 31
|
Conclusion
ZSI, which we completely missed in the last column, turns out to be the
most mature and useable SOAP library for Python. The author expects to
have version 1.2 out soon, which addresses some of the complaints we've
made in this article.
Now that we have surveyed Python SOAP packages, and looked closely at a
couple of them, in the next installment, we will look at how Python
SOAP implementations interoperate with each other.