send POST request to web services in python

2019-08-11 20:28发布

问题:

I need to consume a several's SOAP web services, if I send a xml file as request I get the response without issues. But I want to send only some arguments and not all the xml file

To make a request I send like the using REQUESTS library:

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

The xml file I send as request is like this:

<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>

I get the value of the arguments from a HTML form, the arguments are Identity and BillCycle

<bbs:SubAccessCode>
<bbc:Identity>98705702</bbc:Identity>
</bbs:SubAccessCode>
<bbs:BillCycle>20151001</bbs:BillCycle>

Now I am overwritten a file with the value of the arguments and post the file as request.

I tried send only some arguments or get a list of all the methods from the web service but I get an error using SUDS

suds.transport.TransportError: HTTP Error 403: Forbidden

If possible send only the arguments instead of the whole file using SUDS or REQUESTS?

回答1:

Time has passed, but maybe someone finds it useful:

This is snippet from my project. You can add parameters directly to method.

SUDS is very powerful in that way, and of course check out 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()