Зип с python3 как получить информацию из ответа, - PullRequest
0 голосов
/ 17 января 2020

Мне нужен идентификатор сессии. Он содержит в необработанном виде, но не в объекте напрямую

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>      

1 Ответ

0 голосов
/ 17 января 2020

Вы можете преобразовать ответ из xml в dict с помощью xmltodict lib и затем получить из него ожидаемое значение:

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