• python-Python调用wcf接口


    一、安装suds模块:

    直接用python install suds安装不成功,需要手动安装

    1. 下载suds库,地址:http://pypi.python.org/packages/source/s/suds-jurko/suds-jurko-0.4.1.jurko.4.zip#md5=769689edca81c34c0421a4145b08c264,文件名为:suds-jurko-0.4.1.jurko.4.zip
    2. 解压,产生一个suds-jurko-0.4.1.jurko.4的文件夹
    3. 然后在cmd命令行中,进入到suds-jurko-0.4.1.jurko.4文件夹路径下面,输入命令: python setup.py install
    4. 成功

    二、使用suds实例化client、构造参数

    使用了suds模块,soap协议传输数据

    引用suds模块:from suds.client import Client

    实例化client: client = Client(url)

    构造请求参数(假设请求参数是一个学生类,包含name和age两个属性):

           student= client.factory.create('ClientInfo')

           student.name='miky'

           student.age='18'

    请求接口,假设请求方法为getData:client.service.getData(student,False)(# True仅获取最后末条信息, False 获取所有末条信息)

    三、实例

    一个请求wcf的小工具,输入接口地址和请求方法

     1 from tkinter import *
     2 import tkinter.messagebox as messagebox
     3 from suds.client import Client
     4 import json
     5 
     6 def req(url, method, data):
     7     try:
     8         client = Client(url)
     9     except:
    10         result="调用接口出错"
    11         client=None
    12     if client:
    13         # print(client)查看可调用的wcf方法
    14         code = 'client.service.%s(%s)' % (method, data)
    15         result = eval(code)
    16     req = client.last_sent().str()  # 保存请求报文,因为返回的是一个实例,所以要转换成str
    17     response = client.last_received().str()  # 保存返回报文,把它转换成一个字符串,返回的也是一个实例
    18     WriteRes(method,req,response,data)#调用写入结果函数,把方法名、请求报文、返回报文、和入参传进去
    19     return result
    20 def WriteRes(WsName,req,response,data):
    21     '''
    22     :param WsName: 接口的方法名
    23     :param req: 请求报文
    24     :param response: 返回报文
    25     :param data: 传入的数据
    26     '''
    27     res = response.find(data)#从返回结果里面找data,如果找到的话返回data的下标,也就是索引,找不到的话返回-1
    28     fw_flag = open('WsTestRes.txt','a')#以追加模式打开写入结果文件
    29     if res>0:
    30         fw_flag.write('%s pass
    '%WsName)#如果在返回报文中找到data的话,就写pass,否则就写fail
    31     else:
    32         fw_flag.write('%s fail
    '%WsName)
    33         fw_flag.close()#关闭结果文件
    34         fw_result = open('%s_result.txt'%WsName,'w',encoding='utf-8')#打开以接口方法命名的文件
    35         fw_result.write(req+'
    '*3+response)#保存请求报文和返回报文,
    *3的意思是换行三次
    36         fw_result.close()#关闭结果文件
    37 # r=req('http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl',"getMobileCodeInfo","15116020790")
    38 # print(r)
    39 class Application():
    40     def __init__(self):
    41         self.root=Tk()
    42         self.createWidgets()
    43 
    44     def createWidgets(self):
    45         self.aButton = Button(self.root, text='输入请求接口地址')
    46         self.aButton.pack()
    47         self.urlInput = Entry(self.root,width=80)
    48         self.urlInput.pack()
    49 
    50         self.bButton = Button(self.root, text='输入请求接口方法')
    51         self.bButton.pack()
    52         self.methodInput=Entry(self.root,width=80)
    53         self.methodInput.pack()
    54         self.cButton = Button(self.root, text='输入请求接口参数')
    55         self.cButton.pack()
    56         self.dataInput=Entry(self.root,width=80)
    57         self.dataInput.pack()
    58 
    59         self.alertButton = Button(self.root, text='发起请求', command=self.request_wcf)
    60         self.alertButton.pack()
    61         self.resText = Listbox(self.root,width=80)
    62         self.resText.pack()
    63         # self.quitButton=Button(self.root,text='X',command=self.root.quit)
    64         # self.quitButton.pack
    65 
    66     def request_wcf(self):
    67         #形如http://localhost:8100/GettingStartedLib.ICalculator.svc?wsdl的wcf元数据地址
    68 
    69         url = self.urlInput.get()
    70         #被调用方法
    71         method=self.methodInput.get()
    72         #请求参数,key-value形式
    73         data=self.dataInput.get()
    74         result=req(url,method,data)
    75         self.resText.insert(0,result)
    76         messagebox.showinfo('结果', '请求完成')
    77 
    78 
    79 
    80 app = Application()
    81 # 设置窗口标题:
    82 app.root.title('请求wcf服务工具')
    83 # 主消息循环:
    84 app.root.mainloop()

    入参和返回值都比较简单的情况

  • 相关阅读:
    Shell 脚本基本操作练习
    Unix 环境高级编程---线程创建、同步、
    ubuntu 安装ssh-server时出现错误:openssh-server: Depends: openssh-client (= 1:5.3p1-3ubuntu3) but 1:5.3p1-3ubuntu4 is to be installed
    python set 集合
    python 深浅拷贝
    用户权限管理
    vim 编辑器的使用
    linux系统初体验
    平滑升级nginx
    在windows下如何使用密钥对远程登录服务器?
  • 原文地址:https://www.cnblogs.com/luoyc/p/10682370.html
Copyright © 2020-2023  润新知