打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python编写WebService SOAP之原型
  REST也没有同一的标准,因而也就没有什么快速开发工具可用。REST对查询(任何可转换为数据的东西)和简单的状态修改上比较方便;另外,所谓的方便仅指客户端调用方便,对于服务器端实现来说,都是一样的。  
为什么不用普通的http get方式实现:因为get传递的数据有长度限制。为什么不用普通的http post方式实现:post比get好一点,没有长度的问题,但是post的数据在服务器端解析起来太麻烦。  
用soap实现webservice,可以将数据的解析交给框架去完成。


#### 安装 SOAPpy 库
* 安装 PyXML => $ sudo easy_install PyXML  (0.8.4)
* 安装 fpconst => $ sudo easy_install fpconst (0.7.2)
* 安装 SOAPpy => $ sudo easy_install SOAPpy  (0.12.5)


> No module named pkg_resources  
> curl http://python-distribute.org/distribute_setup.py | python


#### 安装Python ZSI
 $ sudo easy_install ZSI  (2.0)


#### wsdl文件生成相应的 server 端和 client 端的 python 代码框架
ZSI提供了2个脚本(wsdl2py 和 wsdl2dispatch)
$ wsdl2py -b -f yourwsdlfile  
$ wsdl2dispatch -f yourwsdlfile


> 遭遇 wsdl2dispatch unicode problem
> 解决:在wsdl2dispatch.py文件头部加上以下内容


```python
   #coding=utf-8
   import sys
   reload(sys)
   sys.setdefaultencoding('utf8')
```


那么,这时应该多了3个文件,分别是:  


* ``xxx_services.py``
* ``xxx_services_server.py``
* ``xxx_services_types.py``


另外,在windows下可尝试:ZSI文件夹下有个Command.py文件,里面有个wsdl2py的方法,从外部调用。


```python
import commands  
print commands.wsdl2py();    
```


#### 查询request


```python
loc = HTSFinanceServiceLocator()
url = loc.HTSFinancePortType_address
client = HTSFinanceBindingSOAP(url)
req = UserRegRequest()
print "1:request ==>", dir(req)
```
``
request ==> ['Options', 'User', '_Options', '_User', '__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'get_element_Options', 'get_element_User', 'new_Options', 'new_User', 'set_element_Options', 'set_element_User', 'typecode']
``


#### 伪造request


```python
userInfo = req.new_User()
userInfo.set_element_ID(1)
userInfo.set_element_Account("chain@qq.com")
userInfo.set_element_Name("chain")
userInfo.set_element_Password("888888")
req.set_element_User(userInfo)
optionsInfo = req.new_Options()
optionsInfo.set_element_All(False)
fullInfo = optionsInfo.new_Full()
fullInfo.set_element_ID(True)
fullInfo.set_element_Account(True)
fullInfo.set_element_Name(True)
fullInfo.set_element_Password(True)
optionsInfo.set_element_Full(fullInfo)
req.set_element_Options(optionsInfo)


res = client.UserReg(req)
print "2:response ==>", dir(res)
```


问题1  
socket.error: (111, 'Connection refused')


问题2  
ZSI.FaultException: Processing Failure
exceptions:AttributeError
type object 'tuple' has no attribute 'typecode'   
怀疑是框架本身的Bug未被修复http://sourceforge.net/p/pywebsvcs/bugs/279/


为之头疼了2天,终于[在爆栈看到这样一个QA](http://stackoverflow.com/questions/206154/whats-the-best-soap-client-library-for-python-and-where-is-the-documentation-f    "在爆栈看到这样一个QA"),


> ZSI: Very painful to use, and development is slow. Has a module called "SOAPpy", which is different than SOAPy (above).
> SUDS: Very Pythonic, and easy to create WSDL-consuming SOAP clients. Creating SOAP servers is a little bit more difficult.  


于是准备换SUDS做SOAP Client, soaplib做server端。


ZSI用起来问题比较多,前后试了3个版本。当做为服务器端是可以的(还需要忍受非常慢的速度),但同时又要使用客户端就把一些问题暴露出来了。(应该可以使用SUDS编写客户端对服务进行调用,仍需要一定的努力)




#### 安装soaplib 库 (服务器端)  
 
安装soaplib库,它依赖 lxml 和 pytz 这2个库。 而lxml 库又依赖 libxml2 和 libxslt 这2个库。  libxslt 依赖 libxml2 。   以下按序安装… 


* pytz 不依赖任何库,直接从https://pypi.python.org/pypi/pytz/ 下载源码编译安装不折腾。
* libxml2 从(http://xmlsoft.org/sources/)下载源码libxml2-2.9.0.tar.gz,解压后 $ vim ./configure 注释掉$RM "$cfgfile" 所在行 接着一路make && make install。测试安装是否成功 rpm -aq|grep -i libxml2  
* libxslt 从(http://xmlsoft.org/sources/)下载源码libxslt-1.1.27.tar.gz,同上。或$ yum install libxslt-devel  
* lxml 从(https://pypi.python.org/pypi/lxml/3.2.0)下载源码lxml-2.3.0.tar.gz 使用$ sudo python setup.py install 安装。(可用$ python -c "import lxml; " 测试安装是否成功)
* soaplib 从github(https://github.com/volador/soaplib)直接clone下来安装即可。


#### 安装SUDS库 (客户端)
下载地址 https://fedorahosted.org/suds/


#### soaplib开发文档
之后参照此文档(http://soaplib.github.io/soaplib/2_0/#model-api)开发自己的服务函数就好了。用soaplib当开启web server那么会根据url由函数库内部自动生成wsdl文档,比起用ZSI手动生成要科学很多。


======  
参考链接:  


* ZSI项目 http://sourceforge.net/projects/pywebsvcs
* 使用 python 开发 Web Service http://www.ibm.com/developerworks/cn/opensource/os-cn-pythonws/  
* 使用ZSI开发webservice客户端 http://wangrqa.blog.163.com/blog/static/17094682720101115439995/
* ZSI Web Service开发环境配置 http://hi.baidu.com/iwater707/item/bf084a0358a52413cd34ea02
* SUDS项目主页 https://fedorahosted.org/suds/  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python REST讨论(收藏)
SOAP for Python [知识库]
4soaplib:soaplib:pycurl,PIL模块 组建
[018] Android开发之WebService介绍
【原创】用Visual C 客户端调用Weblogic的Webservice
关于动态引用WEB SERVICE的问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服