SOAP and Python ZSI

From PeformIQ Upgrade
Jump to navigation Jump to search

Back to SOAP

References

See http://pywebsvcs.sourceforge.net/ for Python Web Services write-up.


Examples

Hi Folks,

Is there nobody who can help me with this more or less trivial
bit of code?

Greetings,
Sybren

On Sun, Oct 14, 2007 at 11:00:10PM +0200, Sybren Stüvel wrote:
> Hi folks,
> 
> I'm trying to get some complex types to work between a very simple ZSI
> SOAP server and client. So you know what I'm talking about, I've
> placed the code at the bottom of this email.
> 
> When running the server & client, I get this output on the server:
> calling the only exported method 'transfer', I get this (wrapped for
> email)
> 
> $ ./zsi_server.py
> transfer:
>     Positional: ({'dest': u'dst', 'source': u'src', 'Amount':
>            {'currency': u'EUR',
>             'value': u'5322',
>             'fractional_digits': u'2'}},)
>     Named: {}
> localhost - - [14/Oct/2007 22:48:03] "POST / HTTP/1.1" 500 -
> localhost - - [14/Oct/2007 22:48:03] "POST / HTTP/1.1" 200 -
> 
> On the client I get the following output, again wrapped & indented for
> email readability:
> 
> _________________________________ Sun Oct 14 22:48:03 2007 REQUEST:
> <SOAP-ENV:Envelope
>     xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
>     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
>     xmlns:ZSI="http://www.zolera.com/schemas/ZSI/"
>     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>     SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
>   <SOAP-ENV:Header/>
>   <SOAP-ENV:Body>
>     <transfer>
>       <dest id="ob7d95860" xsi:type="xsd:string">dst</dest>
>       <source id="ob7d95820" xsi:type="xsd:string">src</source>
>       <Amount>
>         <currency xsi:type="xsd:string">EUR</currency>
>         <value>5322</value>
>         <fractional_digits>2</fractional_digits>
>       </Amount>
>     </transfer>
>   </SOAP-ENV:Body>
> </SOAP-ENV:Envelope>
> _________________________________ Sun Oct 14 22:48:03 2007 RESPONSE:
> 500
> Internal Server Error
> -------
> Server: ZSI/1.1 BaseHTTP/0.3 Python/2.5.1
> Date: Sun, 14 Oct 2007 20:48:03 GMT
> Content-type: text/xml; charset="utf-8"
> Content-Length: 701
> 
> <SOAP-ENV:Envelope
>     xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
>     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
>     xmlns:ZSI="http://www.zolera.com/schemas/ZSI/"
>     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
>   <SOAP-ENV:Header/>
>   <SOAP-ENV:Body>
>     <SOAP-ENV:Fault>
>       <faultcode>SOAP-ENV:Client</faultcode>
>       <faultstring>Unparseable message</faultstring>
>       <detail>
>         <Eob7925758><ZSI:ParseFaultDetail>
>         <ZSI:string>'int' object has no attribute 'typecode'</ZSI:string>
>         <ZSI:trace></ZSI:trace>
>         </ZSI:ParseFaultDetail>
>     </Eob7925758>
>       </detail>
>     </SOAP-ENV:Fault>
>   </SOAP-ENV:Body>
> </SOAP-ENV:Envelope>
> 
> Traceback (most recent call last):
>   File "./zsi_client.py", line 10, in <module>
>     result = b.transfer(source='src', dest='dst', quantity=amount)
>   File "/usr/lib/python2.5/site-packages/ZSI/client.py", line 64, in __call__
>     **kw)
>   File "/usr/lib/python2.5/site-packages/ZSI/client.py", line 171, in RPC
>     return self.Receive(replytype, **kw)
>   File "/usr/lib/python2.5/site-packages/ZSI/client.py", line 503, in Receive
>     return _Binding.Receive(self, replytype, **kw)
>   File "/usr/lib/python2.5/site-packages/ZSI/client.py", line 431, in Receive
>     raise FaultException(msg)
> ZSI.FaultException: Unparseable message
> <ZSI:ParseFaultDetail>
> <ZSI:string>'int' object has no attribute 'typecode'</ZSI:string>
> <ZSI:trace></ZSI:trace>
> </ZSI:ParseFaultDetail>
> 
> 
> There are a few things weird here. First of all, I pass the third
> parameter using the name 'quantity', but it arrives named 'Amount'.
> How did that happen? Second, why do I get the parameters in a single
> dictionary, instead of passed as simply named parameters? And why
> can't I even return an integer?
> 
> I hope someone can help me out here, since I'm completely at a loss
> how to fix this. To run my code, save the files then start "python
> zsi_server.py" and then "python zsi_client.py". I've done this using
> Python 2.5 and ZSI 2.0.
> 
> Greetings,
> Sybren
> 
> -v--v--v--v--v--v--v--v--v- complextypes.py -v--v--v--v--v--v--v--v--v-
> from ZSI import TC
> 
> TC.TypeCode.typechecks = False
> 
> class Amount(object):
>     def __init__(self, currency, value, fractional_digits):
>         '''Constructor'''
> 
>         self.currency = currency
>         self.value = value
>         self.fractional_digits = fractional_digits
> 
>     def __unicode__(self):
>         return '''%s %.2f''' % (self.currency, float(self))
> 
>     def __str__(self):
>         return unicode(self).encode('utf-8')
> 
>     def __repr__(self):
>         return '''Amount('%s', %i, %i)''' % (self.currency,
>                 self.value, self.fractional_digits)
> 
> Amount.typecode = TC.Struct(Amount, [
>           TC.String('currency'),
>           TC.Integer('value'),
>           TC.Integer('fractional_digits')],
>           'Amount')
> -^--^--^--^--^--^--^--^--^- complextypes.py -^--^--^--^--^--^--^--^--^-
> 
> As you can see, the complex type isn't that complex after all. Here is
> the module with the only exported function:
> 
> -v--v--v--v--v--v--v--v--v- exported.py -v--v--v--v--v--v--v--v--v-
> from complextypes import Amount
> 
> def transfer(*args, **kwargs):
>     print 'transfer:'
>     print '\tPositional: %s' % repr(args)
>     print '\tNamed: %s' % repr(kwargs)
>     return 0
> 
> -^--^--^--^--^--^--^--^--^- exported.py -^--^--^--^--^--^--^--^--^-
> 
> Here are the server and client applications:
> 
> -v--v--v--v--v--v--v--v--v- zsi_server.py -v--v--v--v--v--v--v--v--v-
> #!/usr/bin/env python
> 
> import ZSI.dispatch
> 
> import complextypes
> import exported
> 
> server = ZSI.dispatch.AsServer(
>         port=8081,
>         modules=[exported],
>         typesmodule=complextypes)
> server.server_forever()
> -^--^--^--^--^--^--^--^--^- zsi_server.py -^--^--^--^--^--^--^--^--^-
> 
> -v--v--v--v--v--v--v--v--v- zsi_client.py -v--v--v--v--v--v--v--v--v-
> #!/usr/bin/env python
> 
> from ZSI.client import NamedParamBinding as NPBinding
> import sys
> 
> from complextypes import Amount
> 
> b = NPBinding(url='http://localhost:8081/', tracefile=sys.stdout)
> amount = Amount('EUR', 5322, 2)
> result = b.transfer(source='src', dest='dst', quantity=amount)
> 
> print "Result: %s" % result
> -^--^--^--^--^--^--^--^--^- zsi_client.py -^--^--^--^--^--^--^--^--^-
> 
> 
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems?  Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Pywebsvcs-talk mailing list
> Pywebsvcs-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/pywebsvcs-talk
> Also archived at http://groups.google.com/group/pywebsvcs