Python SOAP客户端嵌套请求

时间:2013-11-25 09:38:31

标签: python soap request

我遇到了Python SOAP请求的问题。 到目前为止,我测试了两个python SOAP客户端库:SUDS和pysimplesoap。 两者都适用于以下示例:

from suds.client import Client
from pysimplesoap.client import SoapClient, SoapFault

# suds example
url = "http://www.webservicex.net/geoipservice.asmx?WSDL"
client = Client(url, cache=None)

print client.service.GetGeoIP((ip))


# pysimplesoap example
client = SoapClient(wsdl="http://www.webservicex.net/geoipservice.asmx?WSDL")

# call the remote method
response = client.GetGeoIP(("10.0.1.152"))

print response

两者都很好,给我预期的回应:

{'GetGeoIPResult': {'ReturnCodeDetails': 'Success', 'IP': '10.0.1.152', 'ReturnCode': 1, 'CountryName': 'Reserved', 'CountryCode': 'ZZZ'}}

使用UI SOAP测试程序,请求如下所示:

-<soap:Envelope>
    -<soap:Body>
        -<GetGeoIP>
            <IPAddress>("10.0.1.152")</IPAddress>
        </GetGeoIP>
    </soap:Body>
</soap:Envelope>

现在的问题是,我需要通过SOAP联系另一个WS,但这不起作用。使用UI SOAP程序(键和令牌可以为空)看起来像:

-<soap:Envelope>
    -<soap:Body>
        -<getNews>
            -<shrequest>
                <data>{'account_number':202VA7, 'track_nr':1757345939}</data>
                <function>getnewsdata</function>
                <keys/>
                <token/>
            </shrequest>
        </getNews>
     </soap:Body>
 </soap:Envelope>

但我的代码不起作用:

url = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client = Client(url, cache=None)

data = "{'account_number':202VA7, 'track_nr':1757345939}"
function = "getnewsdata"
keys = ""
token = ""
shrequest = [data,function,keys,token]

response = client.service.getNews(shrequest)

print response

我明白了:

ValueError('Invalid Args Structure. Errors: %s' % errors)
ValueError: Invalid Args Structure. Errors:

如何正确嵌套我的请求?

1 个答案:

答案 0 :(得分:0)

我终于解决了它。 SUDS图书馆提供某事。很好:

url = "xxxxxxxxxxxxxxxxxxxxxxxxx?wsdl"
client = Client(url, cache=None)

# Creating 'shrequest' obj. before the request
shrequest = client.factory.create('shrequest')
shrequest.data = "{'account_number':202VA7, 'track_nr':1757345939}"
shrequest.function = "getnewsdata"

response = client.service.getShipment(shrequest)

print response