Are There Any Working Examples Of Zolera Soap Infrastructure (zsi)?
Solution 1:
reference: (Thanks to Doug Hellmann ) building soap service
The Zolera Soap Infrastucture (ZSI), is a part of the pywebsvcs project. It provides complete server and client libraries for working with SOAP. To use it, a developer writes the WSDL file (by hand or using a WSDL editor), and then generates Python source for the client and stubs for the server. The data structures defined in the WSDL file are converted into Python classes that can be used in both client and server code.
we implemented a simple echo service that returns as output whatever it gets as input from the client. Listing 1 contains the hand-crafted WSDL inputs for the ZSI version of this service.
Listing 1
<?xml version="1.0" encoding="UTF-8"?><definitionsxmlns="http://schemas.xmlsoap.org/wsdl/"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:tns="urn:ZSI"targetNamespace="urn:ZSI" ><types><xsd:schemaelementFormDefault="qualified"targetNamespace="urn:ZSI"><xsd:elementname="Echo"><xsd:complexType><xsd:sequence><xsd:elementname="value"type="xsd:anyType"/></xsd:sequence></xsd:complexType></xsd:element></xsd:schema></types><messagename="EchoRequest"><partname="parameters"element="tns:Echo" /></message><messagename="EchoResponse"><partname="parameters"element="tns:Echo"/></message><portTypename="EchoServer"><operationname="Echo"><inputmessage="tns:EchoRequest"/><outputmessage="tns:EchoResponse"/></operation></portType><bindingname="EchoServer"type="tns:EchoServer"><soap:bindingstyle="document"transport="http://schemas.xmlsoap.org/soap/http"/><operationname="Echo"><soap:operationsoapAction="Echo"/><input><soap:bodyuse="literal"/></input><output><soap:bodyuse="literal"/></output></operation></binding><servicename="EchoServer"><portname="EchoServer"binding="tns:EchoServer"><soap:addresslocation="http://localhost:7000"/></port></service></definitions>
To generate the client and server code from the WSDL, feed it into the wsdl2py program (included with ZSI). To add support for complex types, add the -b option, but it isn’t required for this simple example. wsdl2py will, in response, produce three files:
Listing 2
EchoServer_client.py is the code needed to build a client for the SimpleEcho web service.
################################################### file: EchoServer_client.py# # client stubs generated by # "ZSI.generate.wsdl2python.WriteServiceModule"# ##################################################from EchoServer_types import *
import urlparse, types
from ZSI.TCcompound import ComplexType, Struct
from ZSI import client
from ZSI.schema import GED, GTD
import ZSI
from ZSI.generate.pyclassimport pyclass_type
# LocatorclassEchoServerLocator:
EchoServer_address = "http://localhost:7000"defgetEchoServerAddress(self):
return EchoServerLocator.EchoServer_address
defgetEchoServer(self, url=None, **kw):
return EchoServerSOAP(
url or EchoServerLocator.EchoServer_address,
**kw)
# MethodsclassEchoServerSOAP:
def__init__(self, url, **kw):
kw.setdefault("readerclass", None)
kw.setdefault("writerclass", None)
# no resource properties
self.binding = client.Binding(url=url, **kw)
# no ws-addressing# op: EchodefEcho(self, request, **kw):
ifisinstance(request, EchoRequest) isFalse:
raise TypeError, "%s incorrect request type" % \
(request.__class__)
# no input wsaction
self.binding.Send(None, None, request, soapaction="Echo", **kw)
# no output wsaction
response = self.binding.Receive(EchoResponse.typecode)
return response
EchoRequest = GED("urn:ZSI", "Echo").pyclassEchoResponse = GED("urn:ZSI", "Echo").pyclass
Listing 3
EchoServer_server.py contains code needed to build the SimpleEcho web service server.
################################################### file: EchoServer_server.py## skeleton generated by # "ZSI.generate.wsdl2dispatch.ServiceModuleWriter"###################################################from ZSI.schema import GED, GTD
from ZSI.TCcompound import ComplexType, Struct
from EchoServer_types import *
from ZSI.ServiceContainer import ServiceSOAPBinding
# Messages
EchoRequest = GED("urn:ZSI", "Echo").pyclassEchoResponse = GED("urn:ZSI", "Echo").pyclass
# Service SkeletonsclassEchoServer(ServiceSOAPBinding):
soapAction = {}
root = {}
def__init__(self, post='', **kw):
ServiceSOAPBinding.__init__(self, post)
defsoap_Echo(self, ps, **kw):
request = ps.Parse(EchoRequest.typecode)
return request,EchoResponse()
soapAction['Echo'] = 'soap_Echo'
root[(EchoRequest.typecode.nspname,EchoRequest.typecode.pname)] = \
'soap_Echo'
Listing 4
EchoServer_types.py has type definitions used by both the client and server code.
################################################### file: EchoServer_types.py## schema types generated by # "ZSI.generate.wsdl2python.WriteServiceModule"###################################################import ZSI
import ZSI.TCcompound
from ZSI.schema import (LocalElementDeclaration, ElementDeclaration,
TypeDefinition, GTD, GED)
from ZSI.generate.pyclassimport pyclass_type
############################### targetNamespace# urn:ZSI##############################classns0:
targetNamespace = "urn:ZSI"classEcho_Dec(ZSI.TCcompound.ComplexType, ElementDeclaration):
literal = "Echo"
schema = "urn:ZSI"def__init__(self, **kw):
ns = ns0.Echo_Dec.schema
TClist = [ZSI.TC.AnyType(pname=(ns,"value"),
aname="_value", minOccurs=1, maxOccurs=1,
nillable=False, typed=False,
encoded=kw.get("encoded"))]
kw["pname"] = ("urn:ZSI","Echo")
kw["aname"] = "_Echo"
self.attribute_typecode_dict = {}
ZSI.TCcompound.ComplexType.__init__(self,None,TClist,
inorder=0,**kw)
classHolder:
__metaclass__ = pyclass_type
typecode = self
def__init__(self):
# pyclass
self._value = Nonereturn
Holder.__name__ = "Echo_Holder"
self.pyclass = Holder
# end class ns0 (tns: urn:ZSI)
Once generated, these files are not meant to be edited, because they will be regenerated as part of a build process whenever the WSDL input changes. The code in the files grows as more types and calls are added to the service definition.
The implementation of the server goes in a separate file that imports the generated code. In the example, the actual service is on lines 18–25 of Listing 5. The @soapmethod decorator defines the input (an EchoRequest) and the output (an EchoResponse) for the call. In the example, the implementation of soap_Echo() just fills in the response value with the request value, and returns both the request and the response. From there, ZSI takes care of building the SOAP response and sending it back to the client.
Listing 5
import os
import sys
from EchoServer_client import *
from ZSI.twisted.wsgi import (SOAPApplication,
soapmethod,
SOAPHandlerChainFactory)
classEchoService(SOAPApplication):
factory = SOAPHandlerChainFactory
wsdl_content = dict(name='Echo',
targetNamespace='urn:echo',
imports=(),
portType='',
)
def__call__(self, env, start_response):
self.env = env
return SOAPApplication.__call__(self, env, start_response)
@soapmethod(EchoRequest.typecode,
EchoResponse.typecode,
operation='Echo',
soapaction='Echo')defsoap_Echo(self, request, response, **kw):
# Just return what was sent
response.Value = request.Value
return request, response
defmain():
from wsgiref.simple_server import make_server
from ZSI.twisted.wsgi import WSGIApplication
application = WSGIApplication()
httpd = make_server('', 7000, application)
application['echo'] = EchoService()
print"listening..."
httpd.serve_forever()
if __name__ == '__main__':
main()
Listing 6 includes a sample of how to use the ZSI client libraries to access the servers from the client end. All that needs to be done is to create a handle to the EchoServer web service, build an EchoRequest, send it off to the web service, and read the response.
from EchoServer_client import *
import sys, time
loc = EchoServerLocator()
port = loc.getEchoServer(url='http://localhost:7000/echo')
print"Echo: ",
msg = EchoRequest()
msg.Value = "Is there an echo in here?"
rsp = port.Echo(msg)
print rsp.Value
Post a Comment for "Are There Any Working Examples Of Zolera Soap Infrastructure (zsi)?"