首页 > 解决方案 > zeep with python3 如何从响应中获取信息,

问题描述

我需要会话 ID。它包含在原始但不直接包含在对象中

from zeep import Client, Settings

settings = Settings(strict=False, xml_huge_tree=True)
client_auth = Client('http://extgate.alean.ru:8082/webservice/ewebsvc.dll/wsdl/IewsServer', settings=settings)
with client_auth.settings(raw_response=True):
    response = client_auth.service.Login(ConnectionID ='',UserAlias = 'Test',Password ='testik', Language ='RU',ProfileID ='',ContextXML='',Timeout ='900000' )
print(response.text)

答案是:

<?xml version="1.0"?>                                                                              <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body SOAP-ENC:encodingStyle="http://schemas.xmlsoap.org/soap/envelope/" xmlns:NS2="urn:webservice-electrasoft-ru:types-ewsServerIntf"><NS1:LoginResponse xmlns:NS1="urn:webservice-electrasoft-ru:types-ewsServerIntf-IewsServer"><return xsi:type="NS2:TewsLoginResult">lrSuccess</return><SessionID xsi:type="xsd:string">{C3EC1993-00B0-4CDE-B26F-68FA0BD4CA76}</SessionID></NS1:LoginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>      

标签: pythonxmlsoapzeep

解决方案


您可以使用lib将响应转换xml为,然后从中获取预期值:dictxmltodict

import xmltodict

response = '<?xml version="1.0"?>                                                                              <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body SOAP-ENC:encodingStyle="http://schemas.xmlsoap.org/soap/envelope/" xmlns:NS2="urn:webservice-electrasoft-ru:types-ewsServerIntf"><NS1:LoginResponse xmlns:NS1="urn:webservice-electrasoft-ru:types-ewsServerIntf-IewsServer"><return xsi:type="NS2:TewsLoginResult">lrSuccess</return><SessionID xsi:type="xsd:string">{C3EC1993-00B0-4CDE-B26F-68FA0BD4CA76}</SessionID></NS1:LoginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>'
data = xmltodict.parse(response)
guid = data['SOAP-ENV:Envelope']['SOAP-ENV:Body']['NS1:LoginResponse']['SessionID']['#text']

输出:

{C3EC1993-00B0-4CDE-B26F-68FA0BD4CA76}

推荐阅读