用Python ZSI写web service的简单例子

ZSI( Zolera Soap Infrastructure) 是现在比较好、开发比较活跃的一个Python WS项目,网址是 http://sourceforge.net/projects/pywebsvcs。

我使用的版本是ZSI-2.0,python2.4,pyxml0.8.4,因为目前2.0版本的文档比较齐全,也应该更稳定一点。

从源代码安装:
python setup.py install

根据文档上的例子简化了一下,调通了,感觉不错。

编写服务器端程序pyws.py,并放到apache的cgi-bin目录下
# !C:/Python24/python.exe
#
首行换成你自己的解释器位置
  

def  hello():
    
return  [ " Hello, world " ]

from  ZSI  import  dispatch
dispatch.AsCGI(rpc
= True)

编写客户端程序
# !C:/Python24/python.exe
#
 -*- coding: utf-8 -*-

import  sys
from  ZSI.client  import  Binding
=  Binding(url = ' http://127.0.0.1/cgi-bin/pyws.py ' , tracefile = sys.stdout)

print  b.hello()

得到的结果如下:

_________________________________ Sat Dec 08 22:02:15 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:Header >
< SOAP-ENV:Body >
    
< hello ></ hello >
</ SOAP-ENV:Body >
</ SOAP-ENV:Envelope >

_________________________________ Sat Dec 08 22:02:15 2007 RESPONSE:
200
OK
-------
Date: Sat, 08 Dec 2007 14:02:15 GMT
Server: Apache/2.0.59 (Win32) mod_python/3.3.1 Python/2.4.3
Content-Length: 468
Content-Type: text/xml; charset="utf-8"

< 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:Header >
< SOAP-ENV:Body >
  
< helloResponse >
    
< Eoa0c070  id ="oa0c070"  xsi:type ="xsd:string" >
    Hello, world
    
</ Eoa0c070 >
  
</ helloResponse >
</ SOAP-ENV:Body >
</ SOAP-ENV:Envelope >
{'Eoa0c070': u'Hello, world'}

你可能感兴趣的:(Web,python,service,header,import,binding)