網站首頁 編程語言 正文
WebService客戶端接口調用及身份驗證問題
最近由于業務需求,需要實現python Webservice的服務以及接口調用。
服務端代碼可自行百度,這里主要描述客戶端以及我遇到的HTTP身份驗證的問題,不多說直接上代碼。
from suds.client import Client
from suds.transport.http import HttpAuthenticated
import base64
import urllib2
# 一:無需身份驗證的簡單調用
url = "http://localhost:8899/?wsdl"
client = Client(url) # 可以print client進行相關信息查看
client.service.methodName(*args) # 方法調用
req = str(client.last_sent()) # 保存請求報文,因為返回的是一個實例,所以要轉換成str
response = str(client.last_received()) # 保存返回報文,返回的也是一個實例
# 二:需要身份驗證的調用
# 1.
client = Client(url=wsdl_url, username=username, password=password)
# 2.
t = HttpAuthenticated(username=username, password=password)
client = Client(url=url, transport=t)
# 3.
t = HttpAuthenticated(username=username, password=password)
t.handler = urllib2.HTTPBasicAuthHandler(t.pm) # 這種我的報錯了,t.pm告知我沒這個對象
t.urlopener = urllib2.build_opener(t.handler)
client = Client(url=wsdl_url, transport=t)
# 4.
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
authenticationHeader = {
"SOAPAction" : "ActionName",
"Authorization" : "Basic %s" % base64string
}
client = Client(url=wsdl_url, headers=authenticationHeader)
這是我針對身份驗證做的一些資料搜集、匯整。
Python調用WebService接口踩坑記錄
應用場景:
?需要與某運營商的某部門進行某些數據的對接,對方扔了一個接口文檔過來,需要根據文檔中的WebService接口規范進行數據的上報。但是在調用對面接口的時候,一直返回500。雖然狀態碼是500,但是根據報錯信息以及給錢就是大爺的原則,最后還是需要調整自身的腳本文件來完成對接。
1.報錯信息
一開始跟大部分帖子使用的第三方庫一樣,我也是使用了suds庫。
#步驟很簡單,導入相關庫,定義url獲取返回即可
import suds
from suds.client import Client
url='xxx'
client=Clinet(url)
result = client.service.xxx('xxx') #第一個xxx是webservice接口中你要調用的函數名,第二個xxx是你要輸入的參數。如果不需要就空著。假如是復雜參數,可以使用client.factory方式來構建。
上述三個步驟就能夠調用最簡單的webservice接口了。
但是。。。。。
按理說是很簡單的一個步驟,沒想到卡我好幾天。
1.1報錯信息一
suds.WebFault: Server raised fault: 'Fault occurred while processing
百度了以后,大部分帖子解釋的錯誤原因是由于上傳的數據中存在空值,需要將空值替換成NULL等等。
所以我就想著是否是數據出了問題,然后檢查了一下自己傳入的數據有沒有少了某個字段,或者某些字段是空著的
然后對著接口文檔一陣猛看,最后發現自己的數據很標準,甚至找到了接口文檔中好些錯別字。。。
然后就懷疑是否跟xml數據的格式有關系,縮進換行啥的。。然后引發了第二個報錯。
1.2報錯信息二
Error reading XMLStreamReader: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs.
?at [row,col {unknown-source}]: [2,5]
這個報錯信息說的是我這邊的xml開頭不規范,但是我一看我的數據:
<?xml version="1.0" encoding="utf-8"?>
這很標準啊,也絲毫沒有問題。。。
然后百度了說是得在數據前后加
<![CDATA[ ?]]>
然后接著一通瞎操作也沒有解決上述兩個報錯的任何一個。。。
但是。。。我最后還是繞過了500,成功調用!
2.解決方法
最后的解決方法就是使用requests庫,不得不說這個庫是真的頂,yyds!!!
webservice接口本質上就是使用HTTP的POST請求,只不過他post過去的是xml格式的數據。之所以很多人使用suds庫是因為該庫能自動組裝好相應xml開頭,比如
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ? ? xmlns:sam="http://service.springboot.huaxun.com/" ? ? xmlns:xsd="http://www.w3.org/2001/XMLSchema" ? ? xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> ? ? <soap:Header/> ? ? <soap:Body> ? ? ? ? xxxxxxxxxxxxxxx ? ? </soap:Body> </soap:Envelope>
下方的代碼實際上是輸入Body中的數據。
result = client.service.xxx('xxx')
然后suds會自動組裝好數據并post出去。
所以理論上,suds庫能完成的requests庫也可以完成,只不過xml得我們自己來組裝了。
造成報錯一的主要原因是命名空間未正確指定,由于網上該庫的教程很少,我也沒能成功從源碼中找到對命名空間的修改方式,所以使用了requests。
url='xxx'
str3='xxx'
header={
?? ?'Content-Type':'text/xml; charset=utf-8',
}
r = requests.post(url,headers=header,data=str3.encode('utf-8'))
print(r)
print(r.text)
打完收工!
有些遺憾的是,仍未成功使用suds調用成功!不清楚如何指定調用函數的namespace。
原文鏈接:https://blog.csdn.net/ajklsdnal/article/details/107611049
相關推薦
- 2022-04-17 python使用openpyxl讀取日期并修改excel
- 2022-07-03 golang defer,func()閉包,panic ,recover,contex
- 2022-06-28 C語言簡明清晰講解枚舉_C 語言
- 2022-10-02 Opencv檢測多個圓形(霍夫圓檢測,輪廓面積篩選)_C 語言
- 2022-09-07 全網非常詳細的pytest配置文件_python
- 2022-04-23 通過CSS的sticky屬性 重新回顧 position
- 2022-05-12 正則判斷只能輸入大于0的正整數
- 2022-01-17 滾動條隱藏后再設置顯示
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支