HI, all:
Recently I am doing something about python webservice. With the help of some groups, most of problems has been solved.
part 1: I need to construct this soapheaders using SUDS.
<serviceCode>PABB4BEIJING</serviceCode>
<servicePwd>QWERTPABB</servicePwd>
</ReqSOAPHeader>
</soapenv:Header>
part 2: my code
import suds
def test_sms9():
#step 1: import a encode
from suds.xsd.doctor import ImportDoctor, Import
d = ImportDoctor(imp)
#step 2: set the logging
import logging
logging.basicConfig(level=logging.ERROR)
# step 3: construt client
client = suds.client.Client(url,doctor=d,cache=None,xstq=False,faults=False)
# step 4: create the header
from suds.sax.element import Element
from suds.sax.attribute import Attribute
code = Element('serviceCode').setText('PABB4BEIJING')
pwd = Element('servicePwd').setText('QWERTPABB')
header_list = [code, pwd]
client.set_options(soapheaders=header_list)
# setp 5: provide the parameters for soap body
item1 = "{'msgid':'1234567890','bizcode':'15140237310','serviceId':'1234567','recomobile':'15110791945','sendtime':'1322573860','content':'hi, this is just a test. you can ignore it. --jiaxiaolei'}"
item2 = "{'msgid':'1234567891','bizcode':'15140237310','serviceId':'1234567','recomobile':'15110791946','sendtime':'1322573870','content':'hi, this is just a test. you can ignore it. --jiaxiaolei'}"
req = [item1, item2]
aoss = client.factory.create('ArrayOf_soapenc_string')
aoss.item = req
print 'client', client
output = client.service.sendMt(aoss)
print 'SOAP Request:\n', client.last_sent(), '\n'
print 'SOAP Response:\n', client.last_received(), '\n'
print 'the return: \n', output, '\n'
#output:
SOAP Request:
<?xml version="1.0" encoding="UTF-8"?>
encoding/">
<SOAP-ENV:Header>
<serviceCode>PABB4BEIJING</serviceCode>
<servicePwd>QWERTPABB</servicePwd>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns0:sendMt>
<mtInfo xsi:type="ArrayOf_soapenc_string">
<item xsi:type="ns1:string">{'msgid':'1234567890','bizcode':'151402
37310','serviceId':'1234567','recomobile':'15110791945','sendtim
e':'1322573860','content':'hi, this is just a test. you can ignore it. --jiaxiaolei&
apos;}</item>
<item xsi:type="ns1:string">{'msgid':'1234567891','bizcode':'151402
37310','serviceId':'1234567','recomobile':'15110791946','sendtim
e':'1322573870','content':'hi, this is just a test. you can ignore it. --jiaxiaolei'}</item>
</mtInfo>
</ns0:sendMt>
</SOAP-ENV:Body>
SOAP Response:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Server.generalException</faultcode>
<faultstring/>
<detail>
<ns1:fault xmlns:ns1="service.global.v1_0.wsdl.protocol.xxt" href="#id0"/>
</detail>
</soapenv:Fault>
<messageId xsi:type="soapenc:string">SEV_0003</messageId>
<text xsi:type="soapenc:string">SoapHeader is null</text>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
the return:
(500, (detail){
fault =
(fault){
_href = "#id0"
}
exceptionName = "xxt.protocol.obj.v1_0.common.ServiceException"
hostname = "ln_xxt_mh01"
})
Then, I modify the code:
# step 4: create the header
from suds.sax.element import Element
from suds.sax.attribute import Attribute
code = Element('serviceCode').setText('PABB4BEIJING')
pwd = Element('servicePwd').setText('QWERTPABB')
header_list = [code, pwd]
client.set_options(soapheaders=header_list)
==>
# step 4: create the header
from suds.sax.element import Element
from suds.sax.attribute import Attribute
code = Element('serviceCode').setText('PABB4BEIJING')
pwd = Element('servicePwd').setText('QWERTPABB')
reqsoapheader = Element('ReqSOAPHeader').insert(code)
reqsoapheader.append(reqsoap_attribute)
client.set_options(soapheaders=reqsoapheader)
# output:
SOAP Request:
<?xml version="1.0" encoding="UTF-8"?>
encoding/">
<SOAP-ENV:Header>
<serviceCode>PABB4BEIJING</serviceCode>
</ReqSOAPHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
.... # NOTE: the body is omitted.
SOAP Response:
# it's same with mentioned above.
I try to change the code to "reqsoapheader = Element('ReqSOAPHeader').insert([code,pwd]) ". it failed.
Last, how to generate a soapheader as :
<SOAP-ENV:Header>
<ReqSOAPHeader>
<serviceCode>xxxx</serviceCode>
<servicePwd>yyy</servicePwd>
</ReqSOAPHeader>
<SOAP-ENV:Header>
Any advice will be highly appreciated. Thanks and regards ahead for the the time and concentration.
Thanks again!
-- Jia Xiaolei