背景
最近使用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 Clientfrom datetime import *#WSDL檔案的Locationurl = "http://host:port/service.wsdl"#根據wsdl建立一個WebService的Clientclient = 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=1monitor1.Interval=1monitor1.Extra=""monitor1.Description="perf1"monitor1.Consolidate=0monitor1.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.ErrMsgelse:task = response1.TaskId#停止任務request2 = client.factory.create('StopRequest')request2.TaskId=taskresponse2 = client.service.StopTask(request2)if response2.ErrCode != 0:print response2.ErrMsg#刪除任務request3 = client.factory.create('DeleteRequest') request3.TaskId=taskresponse3 = 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