使用Python调用Web Service

背景

最近使用GSoap开发了一套基于WebService接口的服务,Java与.Net本身的framework已经集成了WebService的访问组件,C与C++也可以通过GSoap生成客户端代理来访问服务,但是Python本身并没集成WebService的toolkit,下面就Python访问WebService的方法进行介绍。

环境准备

1. 如果python没有安装过setuptool,则需要先下载安装setuptool:
windows下可以从  http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11.win32-py2.6.exe 获取安装包进行安装;
linux下可以从 http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz下载tar包进行源码安装:
tar zxvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py build
python setup.py install
如果是ubuntu或者redhat等系统,也可以使用apt或者rpm方式安装,这里不过多说明,安装过程中可能需要root权限。

2. 检查python中是否安装了suds,在python(version 2.6)交互模式下调用:
>>> from suds.client import Client
如果显示“No module named **”,则表名没有安装过suds,可以从 https://fedorahosted.org/suds/ 获取源码包或者rpm包进行安装,下面介绍一下源码包安装的方法:
tar zxf python-suds-0.4.tar.gz
cd  python-suds-0.4
python setup.py install
同样的,如果python安装在系统目录下,则可能需要root权限。

WebService接口

我本次开发的是一个类似于监控系统,在局域网中有多台主机上安装了probe agent,它们负责收集与回传数据,有一台总控的monitor server,它负责接收用户的Web Service请求,根据请求选择一台或多台probe agent,让其定时收集指定的数据,如cpu空闲率等。monitor server的接口比较简单,主要就是 StartTask,StopTask与DeleteTask,它的WSDL定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="MonitorServer"
 targetNamespace="http://www.baidu.com/dsqa/webservices/"
 xmlns:tns="http://www.baidu.com/dsqa/webservices/"
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:ns="http://www.myserver.com/webservices/"
 xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:MIME="http://schemas.xmlsoap.org/wsdl/mime/"
 xmlns:DIME="http://schemas.xmlsoap.org/ws/2002/04/dime/wsdl/"
 xmlns:WSDL="http://schemas.xmlsoap.org/wsdl/"
 xmlns="http://schemas.xmlsoap.org/wsdl/">

<types>

 <schema targetNamespace="http://www.baidu.com/dsqa/webservices/"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:ns="http://www.myserver.com/webservices/"
  xmlns="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="unqualified"
  attributeFormDefault="unqualified">
  <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
  <complexType name="HostEntry"><!-- ns__HostEntry -->

   <sequence>
     <element name="Endpoint" type="xsd:string" minOccurs="1" maxOccurs="1"/><!-- ns__HostEntry::Endpoint -->
   </sequence>
  </complexType>
  <complexType name="MonitorEntry"><!-- ns__MonitorEntry -->

   <sequence>
     <element name="Name" type="xsd:string" minOccurs="1" maxOccurs="1"/><!-- ns__MonitorEntry::Name -->
     <element name="Description" type="xsd:string" minOccurs="1" maxOccurs="1" default=""/><!-- ns__MonitorEntry::Description -->
     <element name="ValueType" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__MonitorEntry::ValueType -->
     <element name="Interval" type="xsd:unsignedInt" minOccurs="1" maxOccurs="1"/><!-- ns__MonitorEntry::Interval -->
     <element name="Extra" type="xsd:string" minOccurs="1" maxOccurs="1" default=""/><!-- ns__MonitorEntry::Extra -->
     <element name="Consolidate" type="xsd:int" minOccurs="1" maxOccurs="1" default="0"/><!-- ns__MonitorEntry::Consolidate -->
     <element name="Hosts" type="xsd:unsignedLong" minOccurs="0" maxOccurs="unbounded"/><!-- ns__MonitorEntry::Hosts -->
   </sequence>
  </complexType>
  <complexType name="HostArray"><!-- ns__HostArray -->

   <sequence>
    <element name="Items" type="ns:HostEntry" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
   </sequence>
  </complexType>
  <complexType name="MonitorArray"><!-- ns__MonitorArray -->

   <sequence>
    <element name="Items" type="ns:MonitorEntry" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
   </sequence>
  </complexType>
  <complexType name="StartRequest"><!-- ns__StartRequest -->

   <sequence>
     <element name="User" type="xsd:string" minOccurs="1" maxOccurs="1" default="anonymous"/><!-- ns__StartRequest::User -->
     <element name="Description" type="xsd:string" minOccurs="1" maxOccurs="1" default=""/><!-- ns__StartRequest::Description -->
     <element name="MaxProbeTime" type="xsd:dateTime" minOccurs="1" maxOccurs="1"/><!-- ns__StartRequest::MaxProbeTime -->
     <element name="MaxKeepTime" type="xsd:dateTime" minOccurs="1" maxOccurs="1"/><!-- ns__StartRequest::MaxKeepTime -->
     <element name="Force" type="xsd:int" minOccurs="1" maxOccurs="1" default="0"/><!-- ns__StartRequest::Force -->
     <element name="Hosts" type="ns:HostArray" minOccurs="1" maxOccurs="1" nillable="true"/><!-- ns__StartRequest::Hosts -->
     <element name="Monitors" type="ns:MonitorArray" minOccurs="1" maxOccurs="1" nillable="true"/><!-- ns__StartRequest::Monitors -->
   </sequence>
  </complexType>
  <complexType name="StopRequest"><!-- ns__StopRequest -->

   <sequence>
     <element name="User" type="xsd:string" minOccurs="1" maxOccurs="1"/><!-- ns__StopRequest::User -->
     <element name="TaskId" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/><!-- ns__StopRequest::TaskId -->
   </sequence>
  </complexType>
  <complexType name="DeleteRequest"><!-- ns__DeleteRequest -->

   <sequence>
     <element name="User" type="xsd:string" minOccurs="1" maxOccurs="1"/><!-- ns__DeleteRequest::User -->
     <element name="TaskId" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/><!-- ns__DeleteRequest::TaskId -->
   </sequence>
  </complexType>
  <!-- operation request element -->
  <element name="StartTask">
   <complexType>
    <sequence>
     <element name="request" type="ns:StartRequest" minOccurs="0" maxOccurs="1" nillable="true"/><!-- ns__StartTask::request -->
    </sequence>
   </complexType>
  </element>
  <!-- operation response element -->
  <element name="StartResponse">
   <complexType>
    <sequence>
     <element name="ErrCode" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__StartResponse::ErrCode -->
     <element name="ErrMsg" type="xsd:string" minOccurs="1" maxOccurs="1"/><!-- ns__StartResponse::ErrMsg -->
     <element name="TaskId" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/><!-- ns__StartResponse::TaskId -->
    </sequence>
   </complexType>
  </element>
  <!-- operation request element -->
  <element name="StopTask">
   <complexType>
    <sequence>
     <element name="request" type="ns:StopRequest" minOccurs="0" maxOccurs="1" nillable="true"/><!-- ns__StopTask::request -->
    </sequence>
   </complexType>
  </element>
  <!-- operation response element -->
  <element name="StopResponse">
   <complexType>
    <sequence>
     <element name="ErrCode" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__StopResponse::ErrCode -->
     <element name="ErrMsg" type="xsd:string" minOccurs="1" maxOccurs="1"/><!-- ns__StopResponse::ErrMsg -->
    </sequence>
   </complexType>
  </element>
  <!-- operation request element -->
  <element name="DeleteTask">
   <complexType>
    <sequence>
     <element name="request" type="ns:DeleteRequest" minOccurs="0" maxOccurs="1" nillable="true"/><!-- ns__DeleteTask::request -->
    </sequence>
   </complexType>
  </element>
  <!-- operation response element -->
  <element name="DeleteResponse">
   <complexType>
    <sequence>
     <element name="ErrCode" type="xsd:int" minOccurs="1" maxOccurs="1"/><!-- ns__DeleteResponse::ErrCode -->
     <element name="ErrMsg" type="xsd:string" minOccurs="1" maxOccurs="1"/><!-- ns__DeleteResponse::ErrMsg -->
    </sequence>
   </complexType>
  </element>
 </schema>

</types>

<message name="StartTask">
 <part name="parameters" element="ns:StartTask"/><!-- ns__StartTask::ns__StartTask -->
</message>

<message name="StartResponse">
 <part name="parameters" element="ns:StartResponse"/><!-- ns__StartTask::resopne -->
</message>

<message name="StopTask">
 <part name="parameters" element="ns:StopTask"/><!-- ns__StopTask::ns__StopTask -->
</message>

<message name="StopResponse">
 <part name="parameters" element="ns:StopResponse"/><!-- ns__StopTask::response -->
</message>

<message name="DeleteTask">
 <part name="parameters" element="ns:DeleteTask"/><!-- ns__DeleteTask::ns__DeleteTask -->
</message>

<message name="DeleteResponse">
 <part name="parameters" element="ns:DeleteResponse"/><!-- ns__DeleteTask::response -->
</message>

<portType name="MonitorServerPortType">
 <operation name="StartTask">
  <documentation>Service definition of function ns__StartTask</documentation>
  <input message="tns:StartTask"/>
  <output message="tns:StartResponse"/>
 </operation>
 <operation name="StopTask">
  <documentation>Service definition of function ns__StopTask</documentation>
  <input message="tns:StopTask"/>
  <output message="tns:StopResponse"/>
 </operation>
 <operation name="DeleteTask">
  <documentation>Service definition of function ns__DeleteTask</documentation>
  <input message="tns:DeleteTask"/>
  <output message="tns:DeleteResponse"/>
 </operation>
</portType>

<binding name="MonitorServer" type="tns:MonitorServerPortType">
 <SOAP:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
 <operation name="StartTask">
  <SOAP:operation soapAction=""/>
  <input>
     <SOAP:body parts="parameters" use="literal"/>
  </input>
  <output>
     <SOAP:body parts="parameters" use="literal"/>
  </output>
 </operation>
 <operation name="StopTask">
  <SOAP:operation soapAction=""/>
  <input>
     <SOAP:body parts="parameters" use="literal"/>
  </input>
  <output>
     <SOAP:body parts="parameters" use="literal"/>
  </output>
 </operation>
 <operation name="DeleteTask">
  <SOAP:operation soapAction=""/>
  <input>
     <SOAP:body parts="parameters" use="literal"/>
  </input>
  <output>
     <SOAP:body parts="parameters" use="literal"/>
  </output>
 </operation>
</binding>

<service name="MonitorServer">
 <documentation>gSOAP 2.8.4 generated service definition</documentation>
 <port name="MonitorServer" binding="tns:MonitorServer">
  <SOAP:address location="http://host:port"/>
 </port>
</service>

</definitions>

Python调用

#导入suds包
from suds.client import Client
from datetime import *

#WSDL文件的Location
url =  "http://host:port/service.wsdl"
#根据wsdl创建一个WebService的Client
client = Client(url)
request1 = client.factory.create('StartRequest')
request1.User="mycustomer"
request1.Description="test case"
request1.Force=1
#添加一台监控机器
host1 = client.factory.create('HostEntry')
host1.Endpoint='http://myprobe_address'
request1.Hosts.Items.append(host1)

#添加一个监控项
monitor1 = client.factory.create('MonitorEntry') 
monitor1.Name='sys.cpu.idle_rate'
monitor1.ValueType=1
monitor1.Interval=1
monitor1.Extra=""
monitor1.Description="perf1"
monitor1.Consolidate=0
monitor1.Hosts.append(0)
request1.Monitors.Items.append(monitor1)
#请求
request1.MaxKeepTime = datetime.now() + timedelta(seconds=3600)
request1.MaxProbeTime = datetime.now() + timedelta(seconds=60)
response1 = client.service.StartTask(request1)
print response1
#检查响应
if response1.ErrCode != 0:
	print response1.ErrMsg
else:
	task = response1.TaskId

#停止任务
request2 = client.factory.create('StopRequest')
request2.TaskId=task
response2 = client.service.StopTask(request2)
if response2.ErrCode != 0:
	print response2.ErrMsg

#删除任务
request3 = client.factory.create('DeleteRequest') 
request3.TaskId=task
response3 = client.service.DeleteTask(request3)
if response2.ErrCode != 0:
	print response2.ErrMsg

其他

其他几种使用python调用web service的方法: http://pywebsvcs.sourceforge.net/
一些公共的WebService: http://www.webxml.com.cn/zh_cn/web_services.aspx
作者: icefireelf
出处: http://blog.csdn.net/icefireelf/article/details/7264141

你可能感兴趣的:(Web,webservice,python,service,SOAP,Parameters)