ZSI Notes

From PeformIQ Upgrade
Revision as of 10:35, 10 November 2008 by PeterHarding (talk | contribs)
Jump to navigation Jump to search

Some Notes from Pywebsvcs-talk@lists.sourceforge.net List

My scrapbook of notes and extracts from the Pywebsvcs mailing list...

alphabetical order of 'self.attribute_typecode_dict'

This should be irrelevant, although order is important for canonicalization for an entirely different matter.

I think you could retain your ordering by replacing the underlying dict with your own dict subclass that maintains a list too.

	Topic._attrs = OrderDict()


Something like Collection, although obviously this is a bit old.

from ZSI.wstools.Utility import Collection

class Collection(UserDict):
     """Helper class for maintaining ordered named collections."""
     default = lambda self,k: k.name
     def __init__(self, parent, key=None):
         UserDict.__init__(self)
         self.parent = weakref.ref(parent)
         self.list = []
         self._func = key or self.default

     def __getitem__(self, key):
         if type(key) is type(1):
             return self.list[key]
         return self.data[key]

     def __setitem__(self, key, item):
         item.parent = weakref.ref(self)
         self.list.append(item)
         self.data[key] = item

     def keys(self):
         return map(lambda i: self._func(i), self.list)

     def items(self):
         return map(lambda i: (self._func(i), i), self.list)

     def values(self):
         return self.list

Newbie Stuff

Upgrade to ZSI-2.1a and this package is no longer required.

-josh


On May 14, 2008, at 4:02 PM, freeav8r wrote:

> Hi all:
>
> I’m new to ZSI and apache and I’m having
> difficulty getting my first ZSI web service up and
> running.
>
> I installed apache with no problems and also installed
> mod_python.  I ran the mod_python “hello world”
> test by changing the httpd.conf file with the
> following entry:
>
> <Directory "C:/Program Files/Apache Software
> Foundation/Apache2.2/htdocs/test">
>   AddHandler mod_python .py
>   PythonHandler mptest
>   PythonDebug On
> </Directory>
>
> The “Hello world” worked fine.
>
> I tried the mod_python example (2.1.3) found here:
> http://pywebsvcs.sourceforge.net/zsi.html
>
> Because of the apache config entry above, I created
> the following to files with the below names:
>
> ========== mptest.py =========
> from ZSI import dispatch
> from mod_python import apache
>
> import MyHandler
> mod = __import__('encodings.utf_8', globals(),
> locals(), '*')
> mod = __import__('encodings.utf_16_be', globals(),
> locals(), '*')
>
> def handler(req):
>    dispatch.AsHandler(modules=(MyHandler,),
> request=req)
>    return apache.OK
> ========== end mptest.py =======
>
> =========MyHandler.py ========
> def hello():
>    return "Hello, world"
>
> def echo(*args):
>    return args
>
> def average(*args):
>    sum = 0
>    for i in args: sum += i
>    return sum / len(args)
> ========= end MyHandler.py ========
>
> When I request the mptest.py file I get the following
> error:
>
>
>  File "C:\Program
> Files\Python25\Lib\site-packages\ZSI\parse.py", line
> 54, in __init__
>    from xml.dom.ext.reader import PyExpat
>
> ImportError: No module named ext.reader
>
> I'm running python 2.5 and the lastest stable versions
> of apache, mod_python, and ZSI.
>
>
> Any ideas?
>
> -freeav8r

http persistent connection

I think you can do this by using your own transport.

Here is the relevant code from client.py:

         self.h = transport(netloc, None, **self.transdict)
         self.h.connect()
         self.SendSOAPData(soapdata, url, soapaction, **kw)


So basically you'd need something like a Singleton that delegates to  
actual transports, so you can maintain these instances between soap  
calls and reuse the same transport when appropriate.

But I'm not sure about the rest, you'll need a transport that  
implements HTTP persistent connections.  Maybe twisted is a better bet  
than the python httplib.

-josh


On May 17, 2008, at 2:52 PM, Kleber Carriello de Oliveira wrote:

> Sirs,
>
> I'm trying to use ZSI with http persistent connection, does anyone  
> know if it's possible? If yes can you send me one example?
>
> My problem is that I need to call one API about 500 times per  
> second... I believe that using persistent connection I can reach  
> this number instead of open and close one tcp connection each time I  
> do a call.
>
> Thanks in advance,
>
> Kleber Carriello
>
>
>      Abra sua conta no Yahoo! Mail, o único sem limite de espaço  
> para armazenamento!
> http://br.mail.yahoo.com/

ServiceProxy

> server = ServiceProxy(w, pyclass=True, tracefile=sys.stdout)

ZSI 2.0.0 as a whole supports complex type, but there's a serious bug
in ServiceProxy in 2.0.0 which prevents using it with asdict=False
(ServiceProxy in 2.0.0 doesn't have the pyclass keyword) [1]. I
suggest upgrading to 2.1a1 where the broken part of ServiceProxy has
been rewritten and now works.

[1] Specifically ServiceProxy._call always passes args or kwargs
through to binding.Send regardless of what asdict is set to, so that
with complex types calls to proxy methods complain that binding.Send
is getting tuples (i.e. args) or dicts (i.e. kwargs) instead of the
expected complex types.

ZSI Thread Safe?

I don't think so but threads in python are restricted by the GIL so  
all state is shared between threads.  So if you're changing an objects  
state all threads will "instantly" see it.  This is up to you to manage.

-josh


On May 14, 2008, at 12:56 AM, Esteban wrote:

> After some testing, I confirmed that I cannot share the same
> ServicePort on different clients as it is not thread safe (I expected
> this result actually...)
>
> Using a different one for each client seems to work. However, I am
> concerned about race conditions that are difficult to detect. Does
> somebody know whether the servicePorts share any resources that could
> lead to the wrong result if accessing through several threads?
>
> Thanks,
> -- Esteban.
>
> On May 13, 10:01 am, Esteban <robe...@gmail.com> wrote:
>> Hi there,
>>
>> I am writting a client for a Web Service using ZSI 2.0.
>> I have to poll different operations on the same service, using
>> different time intervals.
>> I'd like to use a separate thread for each operation, so that one of
>> them can execute while others are either sleeping or blocked waiting
>> for the service's response.
>>
>> Is the service binding returned by
>> ServiceLocator().getServicePort(url, **kw) thread safe (so that it  
>> can
>> be shared by all client threads)? In case it is not, would it be any
>> problem if I create a different service  locator as above on each
>> thread? I am not sure if they would share any common stuff in
>> downstream...
>>
>> I read a similar post (http://groups.google.com/group/pywebsvcs/
>> browse_thread/thread/cf77b338ccb1e526/26a4e4dcfec57564?
>> lnk=gst&q=thread#26a4e4dcfec57564)
>> but I did not really get it clear and I think it was more focused on
>> the service side (as it talked about Apache for example).
>>
>> Any other suggestion to approach this problem are appreciated ;)
>>
>> Thanks for your help,
>> -- Esteban
>>

Simple Client Example

Joshua Boverhof escribió: 
You can use WSDL too (wsdl2py), but here is a simple client example
-josh


$ 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.client import *
 >>> dir()
['AUTH', 'Address', 'AnyElement', 'AnyType', 'Binding', 'Cookie',  
'EvaluateException', 'FaultException', 'FaultFromFaultMessage',  
'NamedParamBinding', 'ParsedSoap', 'SoapWriter', 'String', 'Struct',  
'TC', 'TypeCode', 'UNICODE_ENCODING', 'WSActionException',  
'ZSI_SCHEMA_URI', '__builtins__', '__doc__', '__name__', 'base64',  
'httplib', 'time', 'types', 'urlparse']

 >>> import sys
 >>> cli = NamedParamBinding("http://www.google.com",  
tracefile=sys.stdout)
 >>> cli.Hello()
_________________________________ Tue May 13 23:09:21 2008 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:Header><SOAP-ENV:Body><Hello></Hello></SOAP- 
ENV:Body></SOAP-ENV:Envelope>
_________________________________ Tue May 13 23:09:22 2008 RESPONSE:
501
Not Implemented
-------
Date: Wed, 14 May 2008 06:09:22 GMT
Content-Type: text/html; charset=UTF-8
Server: gws
Content-Length: 1343
...
...

 >>> cli.Hello(first="Jenn", lastname="Duerr", number=1)
_________________________________ Tue May 13 23:11:03 2008 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:Header><SOAP-ENV:Body><Hello><lastname  
id="o1454e20" xsi:type="xsd:string">Duerr</lastname><number  
id="o803588" xsi:type="xsd:int">1</number><first id="o1454de0"  
xsi:type="xsd:string">Jenn</first></Hello></SOAP-ENV:Body></SOAP- 
ENV:Envelope>
_________________________________ Tue May 13 23:11:03 2008 RESPONSE:
501
Not Implemented
-------
Date: Wed, 14 May 2008 06:11:03 GMT
Content-Type: text/html; charset=UTF-8
Server: gws
Content-Length: 1343
Connection: Close

Wolfram Services

I have dowloaded/installed the following with no errors:
  Python 2.6
  ZSI-2.0
  MingGW-5.1.4
  PyXML-0.8.4

I am running on a PC.

When running the following python code (from ZSI users guide) I get the 
following errors.

Thanks for your HELP

import sys
import timing
from ZSI.generate.commands import wsdl2py

#Connect and build the python files based on wsdl url
#wsdlurl = 
'http://webservices.wolfram.com/services/SearchServices/WolframSearch2.wsdl'
#args = ['-bu', wsdlurl]
#wsdl2py(args)

# import the generated class stubs
from WolframSearchService_services import *
# get a port proxy instance
loc = WolframSearchServiceLocator()
port = loc.getWolframSearchmyPortType()
# create a new request
req = WolframSearchRequest()
req.Options = req.new_Options()
req.Options.Query = 'newton'
# call the remote method
resp = port.WolframSearch(req)    <==== ERROR OCCURS HERE
# print results
print 'Search Time:', resp.Result.SearchTime
print 'Total Matches:', resp.Result.TotalMatches
for hit in resp.Result.Matches.Item:
print hit.Title

---------------------------------------------------------------------------
HERE IS THE ERRORS....

Web Service Access!!

Traceback (most recent call last):

File 
"C:\eclipse\workspace\TestCtsWebService\src\root\examples\wsTestWolframeSearch.py", 
line 30, in <module>

resp = port.WolframSearch(req)

File 
"C:\eclipse\workspace\TestCtsWebService\src\root\examples\WolframSearchService_services.py", 
line 105, in WolframSearch

response = self.binding.Receive(WolframSearchResponse.typecode)

File "C:\Python26\lib\site-packages\ZSI\client.py", line 498, in Receive

self.ReceiveSOAP(**kw)

File "C:\Python26\lib\site-packages\ZSI\client.py", line 398, in ReceiveSOAP

encodingStyle=kw.get('encodingStyle'))

File "C:\Python26\Lib\site-packages\ZSI\parse.py", line 54, in __init__

from xml.dom.ext.reader import PyExpat

ImportError: No module named ext.reader