我需要使用多个 SOAP Web 服务,如果我发送 xml 文件作为请求,我会毫无问题地得到响应。但我只想发送一些参数而不是所有 xml 文件
要发出像使用 REQUESTS 库一样发送的请求:
import requests
with open("/home/WSProject/xmlws/media/QueryTest.xml","r") as
request_data = archivo.read()
target_url = "http://1.1.1.1:4384/services/BbServices?wsdl"
headers = {'Content-type':'text/xml'}
data_response = requests.post(target_url, data=request_data, headers=headers).text
print data_response
我作为请求发送的xml文件是这样的:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bbs="http://example.com/bme/interface/bbservices" xmlns:cbs="http://example.com/bme/interface/cbscommon" xmlns:bbc="http://example.com/bme/interface/bbcommon">
<soapenv:Header/>
<soapenv:Body>
<bbs:QueryCDRRequestMsg>
<RequestHeader>
<cbs:Version>1</cbs:Version>
<!--Optional:-->
<cbs:BusinessCode>1</cbs:BusinessCode>
<cbs:MessageSeq>${=new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(new Date())}</cbs:MessageSeq>
<!--Optional:-->
<cbs:OwnershipInfo>
<cbs:BEID>1</cbs:BEID>
<!--Optional:-->
<cbs:BRID>1</cbs:BRID>
</cbs:OwnershipInfo>
<cbs:AccessSecurity>
<cbs:LoginSystemCode>985</cbs:LoginSystemCode>
<cbs:Password>xyYSFeOmUQ==</cbs:Password>
<!--Optional:-->
<cbs:RemoteIP>1.1.1.1</cbs:RemoteIP>
</cbs:AccessSecurity>
<!--Optional:-->
<cbs:OperatorInfo>
<cbs:OperatorID>5098</cbs:OperatorID>
<!--Optional:-->
<cbs:ChannelID>1</cbs:ChannelID>
</cbs:OperatorInfo>
<cbs:TimeFormat>
<cbs:TimeType>1</cbs:TimeType>
<!--Optional:-->
<cbs:TimeZoneID>1</cbs:TimeZoneID>
</cbs:TimeFormat>
</RequestHeader>
<QueryCDRRequest>
<bbs:SubAccessCode>
<bbc:Identity>98705702</bbc:Identity>
</bbs:SubAccessCode>
<bbs:BillCycle>20151001</bbs:BillCycle>
<bbs:TotalCDRNum>0</bbs:TotalCDRNum>
<bbs:BeginRowNum>0</bbs:BeginRowNum>
<bbs:FetchRowNum>100</bbs:FetchRowNum>
</QueryCDRRequest>
</bbs:QueryCDRRequestMsg>
</soapenv:Body>
</soapenv:Envelope>
我从 HTML 表单中获取参数值,参数是 Identity 和 BillCycle
<bbs:SubAccessCode>
<bbc:Identity>98705702</bbc:Identity>
</bbs:SubAccessCode>
<bbs:BillCycle>20151001</bbs:BillCycle>
现在我用参数值覆盖一个文件,并根据请求发布该文件。
我尝试仅发送一些参数或从 Web 服务获取所有方法的列表,但使用 SUDS 时出现错误
suds.transport.TransportError: HTTP Error 403: Forbidden
如果可能的话,使用 SUDS 或 REQUESTS 仅发送参数而不是整个文件?
最佳答案
时间已经过去了,但也许有人发现它有用:
这是我的项目的片段。您可以直接向方法添加参数。
SUDS 在这方面非常强大,当然请查看 docs .
from Tkinter import *
from suds.client import *
class SoapClass:
def __init__(self, master):
self.client = Client('http://www.webservicex.net/ConvertWeight.asmx?WSDL', username='', password='', faults=False)
Button(master, text='Call', command=self.request).pack()
def request(self):
methodName = 'ConvertWeight'
params = [80, 'Kilograms', 'Grams']
MethodToExecute = getattr(self.client.service, methodName)
try:
response = MethodToExecute(*params)
except WebFault as e:
response = e
print(response)
root = Tk()
app = SoapClass(root)
root.mainloop()
关于python - 在 python 中向 Web 服务发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33876310/