使用zeep.Client Python

时间:2019-06-06 13:23:22

标签: python-3.x soap zeep

我在创建自定义标头并将其添加到SOAP标头时遇到问题。如果我写一个RAW请求,它可以工作,但是我想在Python3中使用zeep库

我试图添加theese标头

header = xsd.Element(
        'headerMDMPeticio',xsd.ComplexType([
            xsd.Element('idSistemaOrigen',xsd.String()),
            xsd.Element(
                'headerMidominio',xsd.ComplexType([
                    xsd.Element('usuariConnectat',xsd.String()),
                    xsd.Element('rolUsuariConnectat',xsd.String())
                ])
            )
        ])
    )
header_value = header(headerMidominio={'usuariConnectat':'user','rolUsuariConnectat':'roleExample'})

摘自泰语书https://buildmedia.readthedocs.org/media/pdf/python-zeep/master/python-zeep.pdf(第18页)

这是用于生成我正在使用的XML的python代码

from django.shortcuts import render
from django.utils import timezone
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin,PermissionRequiredMixin
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView, TemplateView, FormView
from zeep import Client, xsd
from zeep.wsse.username import UsernameToken
from lxml import etree
from .models import MDMRelCodiProjecte

def codiprojecte_list(request):
    codis = MDMRelCodiProjecte.objects.filter(data_esborrat__isnull=True).order_by('data_modificat')
    #Consultamos al servicio SOAP
    url_service = "https://services.dev.midominio.edu/soa/extern/SOAP12/MDMCodisProjectes/v01_00"
    url_wsdl    = url_service+"/?wsdl"

    soapClient = Client(url_wsdl,wsse=UsernameToken('developer_user','miPasswordEnTextoPlano'))
    soapClient.service._binding_options['address'] = url_service # En el WSDL viene con una URL incorrecta --> ponemos la correcta!

    id_service = '100045976'
    soapDataUserPass = {'Username':'developer_user','Password':'miPasswordEnTextoPlano'}

    node = soapClient.create_message(soapClient.service, 'consultarCodiProjecteMDM',id_service)
    print (node)
    print (etree.tostring(node,pretty_print=True))

    #print(soapClient.service.consultarCodiProjecteMDM(id_service,soapDataUsosDada))
    return render(request, 'codi_projecte/codiprojecte_list.html', {'codis':codis})

预期(从SOAP UI中提取),重点关注v01:headerMDMPeticio和该节点的子代。在体内,我还需要添加v012:usosDada来请求。

Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8;action="http://services.dev.midominio.edu/soa/servei/CodisProjectesMDM/v01_00/consultarCodiProjecteMDMInput"
Content-Length: 1811
Host: services.dev.midominio.edu
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v01="http://midominio.edu/soa/esquema/ElementsComunsMDM/v01_00" xmlns:v011="http://midominio.edu/soa/esquema/ElementsComuns/v01_01" xmlns:v012="http://midominio.edu/soa/servei/MDMCodisProjectes/v01_00">
    <soap:Header>
        <wsse:Security soap:mustUnderstand="true" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:UsernameToken wsu:Id="UsernameToken-6e6f6d657365617363757472656e6f6F">
                <wsse:Username>developer_user</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">miPasswordEnTextoPlano</wsse:Password>
                <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">rndomDataHash+rndomtxt==</wsse:Nonce>
                <wsu:Created>2019-06-05T08:20:45.644Z</wsu:Created>
            </wsse:UsernameToken>
        </wsse:Security>
        <v01:headerMDMPeticio>
            <v01:idSistemaOrigen>1</v01:idSistemaOrigen>
            <v01:headerMidominio>
                <v011:usuariConnectat>usuarioPlataforma</v011:usuariConnectat>
                <v011:rolUsuariConnectat>pepitoPalote</v011:rolUsuariConnectat>
            </v01:headerMidominio>
        </v01:headerMDMPeticio>
    </soap:Header>
   <soap:Body>
      <v012:consultarCodiProjecteMDMInput>
         <v012:idMDM>100045976</v012:idMDM>
         <v012:usosDada>
            <v01:arrayUsosDeDadaItem>
               <v01:tipus>Prova persona</v01:tipus>
            </v01:arrayUsosDeDadaItem>
         </v012:usosDada>
      </v012:consultarCodiProjecteMDMInput>
   </soap:Body>
</soap:Envelope

这是我用Python代码生成的

<soap-env:Envelope xmlns:soap-env="http://www.w3.org/2003/05/soap-envelope">
    <soap-env:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>developer_user</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">miPasswordEnTextoPlano</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soap-env:Header>
    <soap-env:Body>
        <ns0:consultarCodiProjecteMDMInput xmlns:ns0="https://services.dev.midominio.edu/soa/servei/MDMCodisProjectes/v01_00">
            <ns0:idMDM>100045976</ns0:idMDM>
        </ns0:consultarCodiProjecteMDMInput>
    </soap-env:Body>
</soap-env:Envelope>

1 个答案:

答案 0 :(得分:2)

Zeep要求您在特殊的kwarg _soapheaders中定义标头,如下所示:

from zeep import Client

wsdl = 'http://localhost/services/Assets?wsdl'

client = Client(wsdl)

request_data = {
    "InterplayPathURI" : "interplay://TGAvid/Projects",
    "SearchGroup" : {
        "Operator" : "OR",
        "AttributeCondition" : {
            "Condition" : "EQUALS",
            "Attribute" :
            {"Group" : "SYSTEM",
             "Name": "Type",
             "_value_1": "masterclip"},
            },
        },
    "MaxResults" : "500",
}

header_value = {
    "credentialsHeader" : {
        "Username" : "administrator",
        "Password" : "Avid123"
    }
}

result = client.service.Search(**request_data, _soapheaders=header_value)