如何通过 python 发送 soap 请求?
How to send soap request by python?
我有 soap 请求,想通过 python zeep 脚本发送。
POST /IntegrationService/IntegrationService.asmx HTTP/1.1
Host: 192.168.66.2
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SendHardwareCommand xmlns="http://parsec.ru/Parsec3IntergationService">
<sessionID>guid</sessionID>
<territoryID>guid</territoryID>
<command>int</command>
</SendHardwareCommand>
</soap12:Body>
</soap12:Envelope>
我试着自己写
from pprint import pprint
from zeep import Client
CODE = '1'
LOGIN = 'PARSEC'
PASSWORD = 'pass'
client = Client('http://192.168.66.2:10101/IntegrationService/IntegrationService.asmx?wsdl', strict=False)
result = client.service.SendHardwareCommand(
SendHardwareCommandRequest={'code': CODE, 'MessageType': 0},
AuthorizationHeader={'login': LOGIN, 'password': PASSWORD})
pprint(result)
如何正确操作?
我在你的 xml 中看不到 Header
的结构,我无法测试你提供的 WSDL,但你可能需要传入你的 login
和 password
值到 _soapheaders()
服务调用中的 kwarg:
# soap_call.py
from pprint import pprint
from zeep import Client
CODE = '1'
LOGIN = 'PARSEC'
PASSWORD = 'pass'
client = Client(
'http://192.168.66.2:10101/IntegrationService/IntegrationService.asmx?wsdl',
strict=False
)
authHeaderRef = client.get_type('<namespace>:AuthorizationHeader')
authHeaderVals = authHeaderRef(login=LOGIN, password=PASSWORD)
result = client.service.SendHardwareCommand(
_soapheaders={AuthorizationHeader=authHeaderVals},
SendHardwareCommandRequest={'code': CODE, 'MessageType': 0})
pprint(result)
更多关于 _soapheaders
here。
请求解决
import requests
territoryid = "91329321-12939213-9-32139-9123"
endpoint = "http://192.168.66.2:10101/IntegrationService/IntegrationService.asmx?wsdl"
user = "opendoor"
password = "password"
body="""<?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>{password}</password>
</OpenSession>
</soap:Body>
</soap:Envelope>"""
body = body.format(user=user, password=password)
body = body.encode('utf-8')
session = requests.session()
session.headers = {"Content-Type": "text/xml; charset=utf-8"}
session.headers.update({"Content-Length": str(len(body))})
response = session.post(url=endpoint, data=body, verify=False)
x=response.content
x=str(x,"UTF-8")
sessionid=x[ x.index("<SessionID>")+11 : x.index("</SessionID>") ]
body="""<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SendHardwareCommand xmlns="http://parsec.ru/Parsec3IntergationService">
<sessionID>{sessionid}</sessionID>
<territoryID>{territoryid}</territoryID>
<command>1</command>
</SendHardwareCommand>
</soap12:Body>
</soap12:Envelope>"""
body = body.format(sessionid=sessionid, territoryid=territoryid)
body = body.encode('utf-8')
session = requests.session()
session.headers = {"Content-Type": "text/xml; charset=utf-8"}
session.headers.update({"Content-Length": str(len(body))})
response = session.post(url=endpoint, data=body, verify=False)
x=response.content
x=str(x,"UTF-8")
print("-------------------------")
#print (x)
from bs4 import BeautifulSoup
xml = response.content
soup = BeautifulSoup(xml, 'xml')
if soup.find_all('Result', text='0'):
print ("door opened")
else:
print("door not opened")
我有 soap 请求,想通过 python zeep 脚本发送。
POST /IntegrationService/IntegrationService.asmx HTTP/1.1
Host: 192.168.66.2
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SendHardwareCommand xmlns="http://parsec.ru/Parsec3IntergationService">
<sessionID>guid</sessionID>
<territoryID>guid</territoryID>
<command>int</command>
</SendHardwareCommand>
</soap12:Body>
</soap12:Envelope>
我试着自己写
from pprint import pprint
from zeep import Client
CODE = '1'
LOGIN = 'PARSEC'
PASSWORD = 'pass'
client = Client('http://192.168.66.2:10101/IntegrationService/IntegrationService.asmx?wsdl', strict=False)
result = client.service.SendHardwareCommand(
SendHardwareCommandRequest={'code': CODE, 'MessageType': 0},
AuthorizationHeader={'login': LOGIN, 'password': PASSWORD})
pprint(result)
如何正确操作?
我在你的 xml 中看不到 Header
的结构,我无法测试你提供的 WSDL,但你可能需要传入你的 login
和 password
值到 _soapheaders()
服务调用中的 kwarg:
# soap_call.py
from pprint import pprint
from zeep import Client
CODE = '1'
LOGIN = 'PARSEC'
PASSWORD = 'pass'
client = Client(
'http://192.168.66.2:10101/IntegrationService/IntegrationService.asmx?wsdl',
strict=False
)
authHeaderRef = client.get_type('<namespace>:AuthorizationHeader')
authHeaderVals = authHeaderRef(login=LOGIN, password=PASSWORD)
result = client.service.SendHardwareCommand(
_soapheaders={AuthorizationHeader=authHeaderVals},
SendHardwareCommandRequest={'code': CODE, 'MessageType': 0})
pprint(result)
更多关于 _soapheaders
here。
请求解决
import requests
territoryid = "91329321-12939213-9-32139-9123"
endpoint = "http://192.168.66.2:10101/IntegrationService/IntegrationService.asmx?wsdl"
user = "opendoor"
password = "password"
body="""<?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>{password}</password>
</OpenSession>
</soap:Body>
</soap:Envelope>"""
body = body.format(user=user, password=password)
body = body.encode('utf-8')
session = requests.session()
session.headers = {"Content-Type": "text/xml; charset=utf-8"}
session.headers.update({"Content-Length": str(len(body))})
response = session.post(url=endpoint, data=body, verify=False)
x=response.content
x=str(x,"UTF-8")
sessionid=x[ x.index("<SessionID>")+11 : x.index("</SessionID>") ]
body="""<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SendHardwareCommand xmlns="http://parsec.ru/Parsec3IntergationService">
<sessionID>{sessionid}</sessionID>
<territoryID>{territoryid}</territoryID>
<command>1</command>
</SendHardwareCommand>
</soap12:Body>
</soap12:Envelope>"""
body = body.format(sessionid=sessionid, territoryid=territoryid)
body = body.encode('utf-8')
session = requests.session()
session.headers = {"Content-Type": "text/xml; charset=utf-8"}
session.headers.update({"Content-Length": str(len(body))})
response = session.post(url=endpoint, data=body, verify=False)
x=response.content
x=str(x,"UTF-8")
print("-------------------------")
#print (x)
from bs4 import BeautifulSoup
xml = response.content
soup = BeautifulSoup(xml, 'xml')
if soup.find_all('Result', text='0'):
print ("door opened")
else:
print("door not opened")