当我使用soapUI 访问Web 服务时,我得到了格式正确的文本。 但是当我使用 python 代码时,我得到一个字典,其中所有行都在一个 allBusType 键中。
from pysimplesoap.client import SoapClient
url = 'http://180.92.171.93:8080/UPSRTCServices/UPSRTCService?wsdl'
namespace = 'http://service.upsrtc.trimax.com/'
client = SoapClient(wsdl=url, namespace=namespace, trace=True)
print client.GetBusTypes()
上面的代码返回以下内容:
{'return': {'allBusType': [{'busName': u'AC SLEEPER'}, {'busType': u'ACS'}, {'ischildconcession': u'N'}, {'isseatlayout': u'N'}, {'isseatnumber': u'N'}, {'busName': u'AC-JANRATH'}, {'busType': u'JNR'}, {'ischildconcession': u'N'}, {'isseatlayout': u'Y'}, {'isseatnumber': u'Y'},....
根据以下屏幕,soapUI 将所有公交车站作为单独的标签返回。 (并不是所有停靠点都在一个标签中,如上所述)
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns3:GetBusTypesResponse xmlns:ns2="com.trimax.upsrtc.xml.jaxb.model" xmlns:ns3="http://service.upsrtc.trimax.com/">
<return>
<allBusType>
<busName>AC SLEEPER</busName>
<busType>ACS</busType>
<ischildconcession>N</ischildconcession>
<isseatlayout>N</isseatlayout>
<isseatnumber>N</isseatnumber>
</allBusType>
<allBusType>
<busName>AC-JANRATH</busName>
<busType>JNR</busType>
<ischildconcession>N</ischildconcession>
<isseatlayout>Y</isseatlayout>
<isseatnumber>Y</isseatnumber>
</allBusType>
我想知道这是Python问题还是服务器问题。
对于每个条目,soapUI 响应中都有名为“allBusType”的开始和结束标记,而 Python 响应中缺少该标记。 Python 输出为所有条目返回一行。
最佳答案
SoapClient返回 SimpleXmlElement
如 SoapClient 文档第一行所述:
A simple, minimal and functional HTTP SOAP webservice consumer, using httplib2 for the connection ad SimpleXmlElement for the XML request/response manipulation.
因此,要将其视为 xml,您需要在返回的 SimpleXmlElement
上调用 as_xml
方法。 :
as_xml(pretty=False): Return the XML representation of the document
以下应该有效:
from pysimplesoap.client import SoapClient
url = 'http://180.92.171.93:8080/UPSRTCServices/UPSRTCService?wsdl'
namespace = 'http://service.upsrtc.trimax.com/'
client = SoapClient(wsdl=url, namespace=namespace, trace=True)
results = client.GetBusTypes()
print results.as_xml()
关于python 响应与soapUI 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29392309/