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\n'%WsName)#如果在返回報文中找到data的話,就寫pass,否則就寫fail
31     else:
32         fw_flag.write('%s fail\n'%WsName)
33         fw_flag.close()#關閉結果文件
34         fw_result = open('%s_result.txt'%WsName,'w',encoding='utf-8')#打開以接口方法命名的文件
35         fw_result.write(req+'\n'*3+response)#保存請求報文和返回報文,\n*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()

 

入參和返回值都比較簡單的情況


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM