Я новичок в python и напишу скрипт, который делает мыльный запрос на сервер и дает мне идентификатор сеанса.
import requests
import re
endpoint = "http://192.168.66.2:10101/IntegrationService/IntegrationService.asmx?wsdl"
body1 = """<?xml version = "1.0" encoding = "utf-8"?>
<soap:Envelope xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema" xmlns:soap = "http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<OpenSession xmlns = "http://parsec.ru/Parsec3IntergationService">
<domain>SYSTEM</domain>
<userName>user</userName>
<password>pass</password>
</OpenSession>
</soap:Body>
</soap:Envelope>"""
body1 = body1.encode('utf-8')
session = requests.session()
session.headers = {"Content-Type": "text/xml; charset=utf-8"}
session.headers.update({"Content-Length": str(len(body1))})
response = session.post(url=endpoint, data=body1, verify=False)
print(response.content)
SESID = re.search(pattern, SessionID)
print (SESID)
выход
b'<?xml version = "1.0" encoding = "utf-8"?><soap:Envelope xmlns:soap = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema"><soap:Body><OpenSessionResponse xmlns = "http://parsec.ru/Parsec3IntergationService"><OpenSessionResult><Result>0</Result><Value><SessionID>d623e923-1aac-4d59-9754-ab279c191860</SessionID><RootOrgUnitID>f7c724d2-7e24-4257-8271-87caca6909f9</RootOrgUnitID><RootTerritoryID>88ef0e32-3b6f-467c-a0ec-0733317f6757</RootTerritoryID></Value></OpenSessionResult></OpenSessionResponse></soap:Body></soap:Envelope>'
Как получить <SessionID>d623e923-1aac-4d59-9754-ab279c191860</SessionID> и добавить его в переменную SESID?






После того, как вы сохраните вывод в переменной, вам необходимо преобразовать его из байтов в строку, используя формат кодировки UTF-8.
Затем вы можете прорезать эту строку, чтобы получить SessionID, как хотите!
x=b'<?xml version = "1.0" encoding = "utf-8"?><soap:Envelope xmlns:soap = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema"><soap:Body><OpenSessionResponse xmlns = "http://parsec.ru/Parsec3IntergationService"><OpenSessionResult><Result>0</Result><Value><SessionID>d623e923-1aac-4d59-9754-ab279c191860</SessionID><RootOrgUnitID>f7c724d2-7e24-4257-8271-87caca6909f9</RootOrgUnitID><RootTerritoryID>88ef0e32-3b6f-467c-a0ec-0733317f6757</RootTerritoryID></Value></OpenSessionResult></OpenSessionResponse></soap:Body></soap:Envelope>'
x=str(x,"UTF-8")
# 12 here is equivalent to len("</SessionID>") which I prefer you'd use
# because you would be able to extract anything else by just replacing the keyword
sid=x[ x.index("<SessionID>") : x.index("</SessionID>")+12 ]
print(sid)
Это даст вам следующий вывод
'<SessionID>d623e923-1aac-4d59-9754-ab279c191860</SessionID>'
О, я делаю это сам "sid=x[ x.index("<SessionID>")+12 : x.index("</SessionID>") ]"
Как я могу получить только d623e923-1aac-4d59-9754-ab279c191860?