python zeep soap动态xml请求

时间:2018-10-22 11:57:03

标签: python json xml wsdl zeep

我有以下python代码:

from requests import Session
from requests.auth import HTTPBasicAuth 
from zeep import Client
from zeep.transports import Transport

session = Session()
session.auth = HTTPBasicAuth("**", "**")
client = Client('wsdl-url',transport=Transport(session=session))

print(client.service.ZSKN_TEST_FM_PY(request,'',''))

wsdl如下:

<?xml version="1.0" encoding="utf-8"?><wsdl:definitions targetNamespace="urn:sap-com:document:sap:rfc:functions" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsoap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="urn:sap-com:document:sap:rfc:functions"><wsdl:documentation><sidl:sidl xmlns:sidl="http://www.sap.com/2007/03/sidl"/></wsdl:documentation><wsdl:types><xsd:schema attributeFormDefault="qualified" targetNamespace="urn:sap-com:document:sap:rfc:functions"><xsd:simpleType name="char15"><xsd:restriction base="xsd:string"><xsd:maxLength value="15"/></xsd:restriction></xsd:simpleType><xsd:simpleType name="clnt3"><xsd:restriction base="xsd:string"><xsd:maxLength value="3"/></xsd:restriction></xsd:simpleType><xsd:simpleType name="string"><xsd:restriction base="xsd:string"/></xsd:simpleType><xsd:complexType name="ZPYTBL_OUT"><xsd:sequence><xsd:element name="ZMANDT" type="tns:clnt3"/><xsd:element name="ZSRNO" type="xsd:short"/><xsd:element name="ZMETHOD" type="tns:char15"/><xsd:element name="ZX1" type="xsd:short"/><xsd:element name="ZY1" type="xsd:short"/><xsd:element name="ZZ1" type="xsd:short"/></xsd:sequence></xsd:complexType><xsd:complexType name="TABLE_OF_ZPYTBL_OUT"><xsd:sequence><xsd:element name="item" type="tns:ZPYTBL_OUT" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence></xsd:complexType><xsd:element name="ZSKN_TEST_FM_PYT"><xsd:complexType><xsd:sequence><xsd:element name="RES_OUTPUT" type="tns:TABLE_OF_ZPYTBL_OUT" minOccurs="0"/><xsd:element name="ZX" type="xsd:short" minOccurs="0"/><xsd:element name="ZY" type="xsd:short" minOccurs="0"/></xsd:sequence></xsd:complexType></xsd:element><xsd:element name="ZSKN_TEST_FM_PYTResponse"><xsd:complexType><xsd:sequence><xsd:element name="RES_OUTPUT" type="tns:TABLE_OF_ZPYTBL_OUT" minOccurs="0"/><xsd:element name="RET_MESG" type="tns:string"/></xsd:sequence></xsd:complexType></xsd:element></xsd:schema></wsdl:types><wsdl:message name="ZSKN_TEST_FM_PYT"><wsdl:part name="parameters" element="tns:ZSKN_TEST_FM_PYT"/></wsdl:message><wsdl:message name="ZSKN_TEST_FM_PYTResponse"><wsdl:part name="parameter" element="tns:ZSKN_TEST_FM_PYTResponse"/></wsdl:message><wsdl:portType name="ZPY_TBL_OUT_WS"><wsdl:documentation><sapdoc:sapdoc xmlns:sapdoc="urn:sap:esi:documentation"><sapdoc:docitem docURL="http://inmumsap20.corp.capgemini.com:8000/sap/bc/esdt/docu/sd_text?sap-client=600&amp;sd_name=ZPY_TBL_OUT_WS"/></sapdoc:sapdoc></wsdl:documentation><wsdl:operation name="ZSKN_TEST_FM_PYT"><wsdl:input message="tns:ZSKN_TEST_FM_PYT"/><wsdl:output message="tns:ZSKN_TEST_FM_PYTResponse"/></wsdl:operation></wsdl:portType><wsdl:binding name="binding" type="tns:ZPY_TBL_OUT_WS"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><wsdl:operation name="ZSKN_TEST_FM_PYT"><soap:operation soapAction="urn:sap-com:document:sap:rfc:functions:ZPY_TBL_OUT_WS:ZSKN_TEST_FM_PYTRequest" style="document"/><wsdl:input><soap:body use="literal"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="service"><wsdl:port name="binding" binding="tns:binding"><soap:address location="http://inmumsap20.corp.capgemini.com:8000/sap/bc/srt/scs/sap/zpy_tbl_out_ws?sap-client=600"/></wsdl:port></wsdl:service></wsdl:definitions>

我想发送如下信息:

<item>
               <ZMANDT>600</ZMANDT>
               <ZSRNO>1</ZSRNO>
               <ZINP>4</ZINP>
            </item>
            <item>
               <ZMANDT>600</ZMANDT>
               <ZSRNO>2</ZSRNO>
               <ZINP>8</ZINP>
            </item>

这样我可以得到一些回应:

<item>
               <ZMANDT>600</ZMANDT>
               <ZSRNO>1</ZSRNO>
               <ZMETHOD>Addtion</ZMETHOD>
               <ZX1>4</ZX1>
               <ZY1>8</ZY1>
               <ZZ1>12</ZZ1>
            </item>
            <item>
               <ZMANDT>600</ZMANDT>
               <ZSRNO>2</ZSRNO>
               <ZMETHOD>Subtraction</ZMETHOD>
               <ZX1>4</ZX1>
               <ZY1>8</ZY1>
               <ZZ1>4</ZZ1>
            </item>
            <item>
               <ZMANDT>600</ZMANDT>
               <ZSRNO>3</ZSRNO>
               <ZMETHOD>Multiplication</ZMETHOD>
               <ZX1>4</ZX1>
               <ZY1>8</ZY1>
               <ZZ1>32</ZZ1>
            </item>
            <item>
               <ZMANDT>600</ZMANDT>
               <ZSRNO>4</ZSRNO>
               <ZMETHOD>Division</ZMETHOD>
               <ZX1>4</ZX1>
               <ZY1>8</ZY1>
               <ZZ1>1</ZZ1>
            </item>

我尝试将json以及xml样式的输入作为reqest参数注入,但未能找到解决方案。请协助。

1 个答案:

答案 0 :(得分:0)

通过使用以下请求json起作用:

request_data = {

'item': [{
    'ZMANDT': '600',
    'ZSRNO': '1',
    'ZINP': '4',
    'ZMETHOD': '',
    'ZX1': '',
    'ZY1': '',
    'ZZ1': ''
},
{
    'ZMANDT': '600',
    'ZSRNO': '2',
    'ZINP': '8',
    'ZMETHOD': '',
    'ZX1': '',
    'ZY1': '',
    'ZZ1': ''
}]