current position:Home>Interface request processing of Python webservice

Interface request processing of Python webservice

2022-05-15 02:37:28Koki can test a rookie

1) introduce suds library , The code is as follows :

from suds.client import Client

2) Create a webservice object , To call webservice Various interfaces inside

webservice Access address

user_url = "http://127.0.0.1:8081/PosMerchant/ZfMposPort"
client = Client(user_url)  # Client There are direct access URL, Can generate a  webservice  object 

3) Print this out wsdl All interface information in the address :# Print out webservice The names of all interface methods inside , The result is shown in the screenshot below :

print(client)

4) utilize soapui( Software tools ) Let's see webservice Composition and parameters of an interface , One of the registered interfaces , As shown below :

'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mpos="http://mpos.zf/">'\
                                    '<soapenv:Header/>'\
                                        '<soapenv:Body>'\
                                            '<mpos:withdrawCash>'\
                                            '<arg0>'+str(t['merCode'])+'</arg0>'\
                                            '<arg1>'+str(t['tranAmt'])+'</arg1>'\
                                            '<arg2>'+str(t['md5Str'])+'</arg2>'\
                                            '<arg3>'+str(t['termID'])+'</arg3>'\
                                            '<arg4>'+str(t['termCode'])+'</arg4>'\
                                            '<arg5>'+str(t['settDate'])+'</arg5>'\
                                            '</mpos:withdrawCash>'\
                                        '</soapenv:Body>'\
                                    '</soapenv:Envelope>'

5) How to pass parameter values : Pass values in a dictionary

t={
    "channel_id":2,"ip":"129.45.6.7","mobile":'mobile' ,"pwd":"123456","user_id" :"shabicu8","verify_code":'123456'}

6) How to be in Python To register this interface service :

client This object , call service This method , Then call userRegister This interface function , Pass the parameter dictionary we just prepared in the function t

result = client.service.userRegister(body)

Print return results

print(result)

7) The final return result is printed as follows :

(result){retCode=‘0’,retInfo = ‘ok’}

Two 、 Write a encapsulated class .

from suds.client import Client
class WebServiceTest:
    def web_service_test(self, url, data, method):
        if method == 'sendMCode':
            res = Client(url).service.sendMCode(data)
        elif method == 'userRegister':
            res = Client(url).service.userRegister(data)
        elif method == 'verifiedUserAuth':
            res = Client(url).service.verifiedUserAuth(data)
        elif method == 'bindBankCard':
            res = Client(url).service.bindBankCard(data)
        return res

#  Create examples 
Client = WebServiceTest()

url = 'http://120.24.235.105:9010/sms-service-war-1.0/ws/smsFacade.ws?wsdl'
data = {
    'client_ip': '192.168.10.66', 'smpl_id': '1', 'mobile': '15096090551'}
res = Client.web_service_test(url, data, 'sendMCode')
print(' The result of SMS sending is :{0}'.format(res))

3、 ... and 、 Traverse all of url Inside webservice request :

from suds.client import Client

class Webservice_Test:
    def __init__(self,url):
        self.client=Client(url)
    def get_methods_name(self):
        method_list = []
        for i in self.client.wsdl.services[0].ports[0].methods:
            method_list.append(i)
        return method_list
    # Get the parameters of the method 
    def get_method_parm(self,method_name):
        method=self.client.wsdl.services[0].ports[0].methods[method_name]
        input_parames=method.binding.input
        params=input_parames.param_defs(method)[0]
        return params[1].name,params[1].type[0]
    def run_main(self):
        for method in self.get_methods_name():
            func=getattr(self.client.service,method)
            print(func('221.112.223.1'))
if __name__ == '__main__':
    url = 'http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl'
    web=Webservice_Test(url)
    # name=web.get_methods_name()[0]
    # print(web.get_method_parm(name))
    print(web.run_main())

copyright notice
author[Koki can test a rookie],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/132/202205120517585527.html

Random recommended