Difference between revisions of "ZSI Examples"
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
Line 506: | Line 506: | ||
</pre> | </pre> | ||
=Authentication= | =Authentication and Zimbra= | ||
<pre> | <pre> |
Latest revision as of 15:16, 22 August 2008
Some Python ZSI Examples
Document Injection Example
Python SOAP - Document Injection Example
SaintBook
$ cat wsdl.sh #!/bin/sh wget http://saintbook.org/MightyMaxims/MightyMaxims.asmx?WSDL
from ZSI.wstools import WSDLTools ... location = 'http://saintbook.org/MightyMaxims/MightyMaxims.asmx?WSDL' load = reader.loadFromURL wsdl = load(location) ...
Not sure works for me -josh
$ mkdir TMP $ cd TMP/ $ wsdl2py http://saintbook.org/MightyMaxims/MightyMaxims.asmx?WSDL $ ls Maxim_client.py Maxim_server.py Maxim_types.py $ python Python 2.5.1 (r251:54863, May 23 2007, 10:06:54) [GCC 4.0.0 20041026 (Apple Computer, Inc. build 4061)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from ZSI.wstools import WSDLTools >>> location = 'http://saintbook.org/MightyMaxims/MightyMaxims.asmx?WSDL' >>> w = reader.loadFromURL(location) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'reader' is not defined >>> reader = WSDLTools.WSDLReader() >>> w = reader.loadFromURL(location) >>> w <ZSI.wstools.WSDLTools.WSDL instance at 0xca080>
On Jan 31, 2008, at 7:49 AM, Lorena Carlo wrote: > Hi, > > I don't know if this is a ZSI bug, but for some reason I can not load > the following webservice: > > http://saintbook.org/MightyMaxims/MightyMaxims.asmx?WSDL > > This is the portion of code: > > from ZSI.wstools import WSDLTools > location = 'http://saintbook.org/MightyMaxims/MightyMaxims.asmx?WSDL' > load = reader.loadFromURL > wsdl = load(location) > > This is the exception I get: > > Traceback (most recent call last): > File "<stdin>", line 1, in ? > File > "c:\Python24\lib\site-packages\zsi-2.1_a1-py2.4.egg\ZSI\wstools > \WSDLTools > .py", line 39, in loadFromURL > File > "c:\Python24\lib\site-packages\zsi-2.1_a1-py2.4.egg\ZSI\wstools > \Utility.p > y", line 642, in loadFromURL > File > "c:\Python24\lib\site-packages\zsi-2.1_a1-py2.4.egg\ZSI\wstools > \Utility.p > y", line 203, in urlopen > File > "c:\Python24\lib\site-packages\zsi-2.1_a1-py2.4.egg\ZSI\wstools > \Utility.p > y", line 153, in urlopen > File "c:\Python24\lib\urllib.py", line 82, in urlopen > return opener.open(url) > File "c:\Python24\lib\urllib.py", line 190, in open > return getattr(self, name)(url) > File "c:\Python24\lib\urllib.py", line 426, in open_file > return self.open_local_file(url) > File "c:\Python24\lib\urllib.py", line 440, in open_local_file > raise IOError(e.errno, e.strerror, e.filename) > IOError: [Errno 2] No such file or directory: > '\\MightyMaxims\\MightyMaxims.asmx > ?WSDL?65784000' > > Any help will be appreciated, thanks! > > Lorena
Building Complex Type Arrays
So grab an interpreter and import the generated module, then use help() to look at the request you're trying to use. You'll probably see some helper methods and properties in there, new_ set_ get_ If that doesn't help provide the WSDL -josh
On Feb 6, 2008, at 7:51 AM, Joseph Boiteau wrote:
> Hi, > > I'm using a ZSI client interface generated with --complexType > option (ZSI 2.0) > When I try to construct a request properly, I cannot find a nice way > to instantiate the array object. > > A simple list [] seems to correspond to the array, but then I have > no parent object to instantiate an array element.. > > Using the second generated file (*_services_types.py), the element i > get using ns0.myDef_Def(None) is a basic object.. without specific > attributes or methods I need. > > So I'm looking after the manner to instantiate properly any sub-node > object. > > Thanks
Nilable Types
a nilled element looks like this: <whatever xsi:nil="true"/> So it should be present if it is minOccurs=1 -josh On Feb 7, 2008, at 11:32 AM, Rex Corrovan wrote: Hey all, Been using ZSI a while now, and came across a 'Bad Thing™'. I would get an exception that 'Required <type> missing' any time I would make certain requests. Upon further investigation I realized that if the WSDL I was using had nillable=true, and minOccurs=1 ZSI, and more specifically TC.py, would throw this exception if the server returned an empty element. In this case an Empty element is valid because nillable is true, and the server HAS to return this tag simply because it has minOccurs of 1. Anyway, the fix was not to hard, on line (note: I also added the space after 'Required ' in the exception string for a better looking message) from original Line 153: if not href: if self.minOccurs is 0: return None raise EvaluateException('Required' + tag + ' missing', ps.Backtrace(elt)) To my patched version: if not href: if self.minOccurs is 0: return None if self.minOccurs is 1 and self.nillable is True: return None raise EvaluateException('Required ' + tag + ' missing', ps.Backtrace(elt))
Namespaces
This is the wsdl for the above service: <?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.2_01-hudson-189-. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.2_01-hudson-189-. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://services/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://services/" name="testserviceService"> <wsp:UsingPolicy></wsp:UsingPolicy> <wsp:Policy wsu:Id="testservicePortBinding_testMethod_WSAT_Policy"> <wsp:ExactlyOne> <wsp:All> <ns1:ATAlwaysCapability xmlns:ns1="http://schemas.xmlsoap.org/ws/2004/10/wsat" wsp:Optional="false"></ns1:ATAlwaysCapability> <ns2:ATAssertion xmlns:ns3="http://schemas.xmlsoap.org/ws/2002/12/policy" xmlns:ns2="http://schemas.xmlsoap.org/ws/2004/10/wsat" ns3:Optional="true" wsp:Optional="true"></ns2:ATAssertion> </wsp:All> </wsp:ExactlyOne> </wsp:Policy> <types> <xsd:schema> <xsd:import namespace="http://services/" schemaLocation="http://localhost:8080/testserviceService/testservice?xsd=1"></xsd:import> </xsd:schema> </types> <message name="testMethod"> <part name="parameters" element="tns:testMethod"></part> </message> <message name="testMethodResponse"> <part name="parameters" element="tns:testMethodResponse"></part> </message> <portType name="testservice"> <operation name="testMethod"> <input message="tns:testMethod"></input> <output message="tns:testMethodResponse"></output> </operation> </portType> <binding name="testservicePortBinding" type="tns:testservice"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding> <operation name="testMethod"> <wsp:PolicyReference URI="#testservicePortBinding_testMethod_WSAT_Policy"></wsp:PolicyReference> <soap:operation soapAction=""></soap:operation> <input> <soap:body use="literal"></soap:body> </input> <output> <soap:body use="literal"></soap:body> </output> </operation> </binding> <service name="testserviceService"> <port name="testservicePort" binding="tns:testservicePortBinding"> <soap:address location="http://localhost:8080/testserviceService/testservice"></soap:address> </port> </service> </definitions>
(This code directly incooperates an EJB, but without this, directly having the code in the WebMethod shows the same result.)
Using wsdl2py I got:
wn@omega:~$ wsdl2py --url http://localhost:8080/testserviceService/testservice?wsdl Error loading http://localhost:8080/testserviceService/testservice?wsdl: namespace of schema and import match wn@omega:~$
Using the ServiceProxy module directly in the code shows the same result.
Could someone give me a hint what's wrong here?
wsdl2py Example
certainly it is a stupid failure, but I'm unable to bring it to work: a WebService provider coded using Netbeans with J2EE 1.5 and the client with ZSI:
wn@omega:~$ wsdl2py --url http://localhost:8080/testserviceService/testservice?wsdl Error loading http://localhost:8080/testserviceService/testservice?wsdl: namespace of schema and import match wn@omega:~$
Using the ServiceProxy module directly in the code shows the same result.
From your wsdl .....
<xsd:import namespace="http://services/" schemaLocation="http://localhost:8080/testserviceService/testservice?xsd=1"></xsd:import>
sugarsoap - Client 1
$ cat run.py #!/usr/bin/env python from sugarsoap_services import * import md5 import sys def main(): loc = sugarsoapLocator() portType = loc.getsugarsoapPortType(tracefile=sys.stdout) request = loginRequest() request._user_auth = uauth = request.new_user_auth() request._application_name='appname' uauth._user_name ='admin' uauth._password = md5.new('password').hexdigest() uauth._version = '1.1' response = portType.login(request) print response._return print response._return._id print response._return._error._name if "__main__" == __name__: main()
sugarsoap - Client 1
$ cat run.py #!/usr/bin/env python from sugarsoap_services import * import md5 import sys def main(): loc = sugarsoapLocator() portType = loc.getsugarsoapPortType(tracefile=sys.stdout) request = loginRequest() request._user_auth = uauth = request.new_user_auth() request._application_name='appname' uauth._user_name ='admin' uauth._password = md5.new('password').hexdigest() uauth._version = '1.1' response = portType.login(request) print response._return print response._return._id print response._return._error._name if "__main__" == __name__: main()
certainly it is a stupid failure, but I'm unable to bring it to work: a WebService provider coded using Netbeans with J2EE 1.5 and the client with ZSI:
wn@omega:~$ wsdl2py --url http://localhost:8080/testserviceService/testservice?wsdl Error loading http://localhost:8080/testserviceService/testservice?wsdl: namespace of schema and import match wn@omega:~$
Using the ServiceProxy module directly in the code shows the same result.
From your wsdl .....
<xsd:import namespace="http://services/" schemaLocation="http://localhost:8080/testserviceService/testservice?xsd=1"></xsd:import>
sugarsoap - Client 1
$ cat run.py #!/usr/bin/env python from sugarsoap_services import * import md5 import sys def main(): loc = sugarsoapLocator() portType = loc.getsugarsoapPortType(tracefile=sys.stdout) request = loginRequest() request._user_auth = uauth = request.new_user_auth() request._application_name='appname' uauth._user_name ='admin' uauth._password = md5.new('password').hexdigest() uauth._version = '1.1' response = portType.login(request) print response._return print response._return._id print response._return._error._name if "__main__" == __name__: main()
An import's namespace must match the targetNamespace attribute of the "file" or schema being imported.
But this namespace must be different than the namespace of the schema you are importing it into.
// <schema targetNamespace="urn:1"> <import namespace="urn:2" schemaLocation="importFile.xsd:/> // importFile.xsd <schema targetNamespace="urn:2"> ...
-josh
Nusoap
from ZSI import * pyobj = [ 1, 2.0, 'a', ['b', 1.0], [(4,'a'), (5, 'c')] ], sw = writer.SoapWriter() sw.serialize(pyobj, typecode=Any(), unique=True) str(sw) import sys cli = client.Binding(url='', tracefile=sys.stdout) rsp = cli.whatever(pyobj) Re: ZSI client, Nusoap server - data format trouble? Subject: Re: ZSI client, Nusoap server - data format trouble? List-id: Discussion of Python and Web services <pywebsvcs-talk.lists.sourceforge.net> ZSI-1.7 (or whatever) is corrupting the XML instance by using the default namespace, I think if you use ZSI-2.* or svn it will work. -josh
sugarsoap
sugarsoap - Client
$ cat run.py #!/usr/bin/env python from sugarsoap_services import * import md5 import sys def main(): loc = sugarsoapLocator() portType = loc.getsugarsoapPortType(tracefile=sys.stdout) request = loginRequest() request._user_auth = uauth = request.new_user_auth() request._application_name='appname' uauth._user_name ='admin' uauth._password = md5.new('password').hexdigest() uauth._version = '1.1' response = portType.login(request) print response._return print response._return._id print response._return._error._name if "__main__" == __name__: main()
sugarsoap - Client 2
#!/usr/bin/env python from sugarsoap_services import * import md5 import sys def main(): loc = sugarsoapLocator() portType = loc.getsugarsoapPortType(tracefile=sys.stderr) uauth = ns1.user_auth_Def() uauth._user_name ='admin' uauth._password = md5.new('password').hexdigest() uauth._version = '1.1' request = loginRequestWrapper() request._user_auth = uauth request._application_name='appname' response = portType.login(request) print response._return print response._return._id print response._return._error._name if "__main__" == __name__: main()
Authentication and Zimbra
I posted recently about type problems when using ZSI with a service (Zimbra Collaboration Suite, or ZCS) that does not have WSDL available: http://sourceforge.net/mailarchive/forum.php?thread_name=20080716203904.GA7422%40boost.horde.net&forum_name=pywebsvcs-talk We were able to get an unofficial/mostly complete WSDL from Zimbra. I had problems using ZSI 2.0 with it, since ZCS relies on SOAP Headers to pass an authentication token. SOAP Header support was added in ZSI 2.1alpha1, which works fine. Below is some sample code. john binding = zimbraAdminLocator().getzimbraAdminSoap12( url='https://vicar.example.com:7071/service/admin/soap') auth_req = zimbra_admin_server.AuthSoap12In() auth_req._name = 'admin' auth_req._password = 'examplepass' auth_res = binding.Auth(authreq) context = GED('urn:zimbra', 'context').pyclass() context._authToken = auth_res._authToken context._sessionId = auth_res._sessionId create_req = zimbra_admin_server.CreateAccountSoap12In() create_req._name = 'exampleuser@vicar.example.com' create_req._password = 'userpass' binding.CreateAccount(create_req, soapheaders=[context]) spec = GTD('urn:zimbraAdmin', 'GetAccountSpecifier')(None).pyclass( 'exampleuser@vicar.example.com') spec._attrs = {'by': 'name'} get_req = zimbra_admin_server.GetAccountSoap12In() get_req._account = spec acct = binding.GetAccount(get_req, soapheaders=[context]) id = acct._account._attrs['id'] fwd_addr = GTD('urn:zimbraAdmin', 'ItemAttribute')(None).pyclass( 'forwarddest@example.com') fwd_addr._attrs = {'n': 'zimbraPrefMailForwardingAddress'} modify_req = zimbra_admin_server.ModifyAccountSoap12In() modify_req._id = id modify_req._a = [fwd_addr] binding.ModifyAccount(modify_req, soapheaders=[context]) -- John Morrissey _o /\ ---- __o jwm@horde.net _-< \_ / \ ---- < \, www.horde.net/ __(_)/_(_)________/ \_______(_) /_(_)__