November 15, 2008

Python Web Services - HOW TO 3 - ZSI Client

I tried to explain how to write web service server code with Python using ZSI and XML-RPC before. Now i want to explain how to write a web service client code with Python using ZSI.

We may remember that when we use wsdl2py command, there comes 3 files those names are;

SillyWebService_server.py

SillyWebService_client.py

SillyWebService_types.py

and also if we remember, we import SillyWebService_server while writing our web service server code. So this time, we import SillyWebService_client and other necessary modules to our client code;

#!/usr/bin/python

import sys

from ZSI.client import Binding

from optparse import OptionParser

from SillyWebService_client import *

After that, we define a function that takes arguments as web service's function;

def tryWebService(param):

request = sillyfunctionRequest(param = param)

response = service.sillyfunction(request)

print response._return

And then we write down the code that takes url from console and make a request to it and write the whole response to the console;

op = OptionParser(usage="%prog [options]")

op.add_option("-u", "--url", help="service URL", metavar="URL")

options, args = op.parse_args()

service = SillyWebServiceLocator().getSillyWebServicePort(url=options.url, tracefile=sys.stdout)

At the end we call the function and return to the console.

tryWebService("hello world!")

I know that, this code is so simple and has no object oriented approach. I write it only to test the web service server i wrote down. So if you want a better client code, you can look up here.

To run the client code, i assume we saved it as 'client.py', you just open the console;

python client.py -u http://localhost:8181/SillyWebService

I write down the url as i define it at this article i write. Also here is the sample code.

No comments: