最近看Python做WebService的客户端,google出结果不少,很不给力,罗罗嗦嗦一堆。
哪有那么复杂,其实很简单。直接上代码
服务器端,我用asp.net做了一个简单的WebService,就4个方法。代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace MyWebService { /// <summary> /// Summary description for Service1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class GetTimeService : System.Web.Services.WebService { [WebMethod] public string GetDay() { return DateTime.Now.Day.ToString(); } [WebMethod] public string GetWeekDay() { return DateTime.Now.DayOfWeek.ToString(); } [WebMethod] public string GetMonth() { return DateTime.Now.Month.ToString(); } [WebMethod] public string UploadFile(byte[] file) { return file.Length.ToString(); } } }
三个方法很简单,就是一个get时间的方法。最后一个是类似于上传文件,接受一个byte的数组。
然后把WebService部署到IIS上,得到他的WSDl如下:
<?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> <s:element name="GetDay"> <s:complexType /> </s:element> <s:element name="GetDayResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="GetDayResult" type="s:string" /> </s:sequence> 。。。。。。。。 <soap12:body use="literal" /> </wsdl:input> <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="GetTimeService"> <wsdl:port name="GetTimeServiceSoap" binding="tns:GetTimeServiceSoap"> <soap:address location="http://localhost/MyWebService/GetTimeService.asmx" /> </wsdl:port> <wsdl:port name="GetTimeServiceSoap12" binding="tns:GetTimeServiceSoap12"> <soap12:address location="http://localhost/MyWebService/GetTimeService.asmx" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
然后网上一直说可以Python wsdl2py生成python文件,可惜我一直不行,但是找到了ZSI文件夹下有个Command.py文件,里面有个wsdl2py的方法,然后我自己写了一个入口函数,call了一下它,
import commands print "begin the process" files=commands.wsdl2py(); print files
顺利在generate文件夹下生成了三个文件:××_server.py ××_client.py ××_types.py,其实和visualstudio添加web ref时候生成的一堆文件是一样的作用,就是为本地call web service搭桥。
然后写一个python文件测试一下这几个生成的文件:
__author__ = 'zbo' from GetTimeService_client import *; from GetTimeService_server import *; from GetTimeService_types import *; locator=GetTimeServiceLocator(); address=locator.getGetTimeServiceSoapAddress(); print address ##initial the webservice worker SOAPWorker=GetTimeServiceSoapSOAP(address) def testGetDay(): getdaysopein=GetDaySoapIn() print getdaysopein getdayresponse=SOAPWorker.GetDay(getdaysopein); print getdayresponse._GetDayResult def testGetMonth(): getmonthsopein=GetMonthSoapIn() print getmonthsopein getmonthresponse=SOAPWorker.GetMonth(getmonthsopein); print getmonthresponse._GetMonthResult def testGetWeekDay(): getweekdaysopein=GetWeekDaySoapIn() print getweekdaysopein getweekdayresponse=SOAPWorker.GetWeekDay(getweekdaysopein); print getweekdayresponse._GetWeekDayResult; def testUploadFile(): uploadfilesopein=UploadFileSoapIn(); print uploadfilesopein; uploadfilesopein._file='zbo'; uploadfileresponse=SOAPWorker.UploadFile(uploadfilesopein); print uploadfileresponse._UploadFileResult; testGetDay(); testGetMonth(); testGetWeekDay(); testUploadFile();
很顺利就通过了。