The application of Python in WebService interface test

Source: Internet
Author: User
Tags wsdl

Interface Test Second wave, WebService interface come on, welcome to the small partners spit Groove ~
This time take free internet domestic mobile phone number attribution to query Web Service WebService interface Do example, of course there are a lot of free WebService interface for everyone to use, Baidu a bit more n, mobile phone number attribution to query WSDL address as follows:/HTTP webservice.webxml.com.cn/webservices/mobilecodews.asmx?wsdl
When opened, you can see that there is a Getmobilecodeinfo method, the entry is a phone number, of course, you can also use Python to see, the next introduction. Using Python's Suds module, which is a third-party module that needs to be installed, if Setuptools or PIP is installed, it can be installed directly with the Easy_install or PIP command, Easy_install Suds or pip install Suds , if not installed, you can go to the official web download, http://pypi.python.org/pypi/suds, after downloading into the Suds directory Python setup.py install.
Because each interface method is different, the import parameter is not the same, so there is no way to write a generic class like the HTTP rest interface, you can only modify the interface's method name and the entry parameter when testing.
First, let's take a look at some of the methods under this WSDL: from suds.client import client# to the client class under the Suds.client module
Client = client () #创建一个wsdl对象
Print client# Prints the details of this object as follows:

Suds (https://fedorahosted.org/suds/) version:0.4 GA build:r699-20100913
Service (mobilecodews) tns= "http://WebXml.com.cn/"
Prefixes (1)
NS0 = "http://WebXml.com.cn/"
Ports (2):
(MOBILECODEWSSOAP)
Methods (2):
GetDatabaseInfo ()
Getmobilecodeinfo (xs:string mobilecode, xs:string UserID,)
Types (1):
Arrayofstring
(MOBILECODEWSSOAP12)
Methods (2):
GetDatabaseInfo ()
Getmobilecodeinfo (xs:string mobilecode, xs:string UserID,)
Types (1):
Arrayofstring
=================================================

From the above results can be seen there are two methods one is GetDatabaseInfo () No entry, one is Getmobilecodeinfo (), the entry is a string, the phone number and user ID, the user ID of the free user can not fill, the following is the code used to test, The comments are also very detailed:

123456789101112131415161718192021222324252627282930313233343536 from Suds. Client Import Client #导入suds. Client class under the client module Mobile_url="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"#手机号码归属地 Qq_url="http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"#qq在线状态 random_url=' http://webservice.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl '# Generate random string def wstest(url,wsname,data): " ":p Aram URL:WSDL address:p Aram Wsname: Method name, file name to save the result:p Aram Data: Incoming parameters of the method: return:" " client = client(URL)#创建一个webservice接口对象 client. Service. Getmobilecodeinfo(data)#调用这个接口下的getMobileCodeInfo方法, and pass in the parameters     req = str (client. Last_sent () ) # Save the request message because an instance is returned, so convert to str response = str(client. Last_received())#保存返回报文, convert it to a string and return an instance Print(response)#打印返回报文 writeres(wsname,req,response,data)#调用写入结果函数, the method name, request message, return message, and enter into the argument .def writeres(wsname,req,response,data): " ":p Aram Wsname: Method Name of the interface:p Aram Req: Request message:p Aram Response: Return message:p Aram Data: Incoming" " res = response. The Find(data)#从返回结果里面找data, if found, returns the subscript of data, which is the index, which cannot be found. 1 fw_flag = open('/tmp/wstestres/wstestres.txt ',' a ')# Open write Results file in append mode If res>0: fw_flag. Write('%s pass '%wsname)#如果在返回报文中找到data的话, writing pass, otherwise write fail Else: fw_flag. Write('%s fail '%wsname) fw_flag. Close()#关闭结果文件 fw_result = open('/tmp/wstestres/%s_result.txt '%wsname, ' W ')#打开以接口方法命名的文件 fw_result. Write(req+' \ n '*3+response)#保存请求报文和返回报文, \n*3 means three-time line break fw_result. Close()#关闭结果文件 if __name__ = =' __main__ ': wstest(mobile_url,' Getmobilecodeinfo ',' a ')

The application of Python in WebService interface test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.