-1

There is a lot of questions with same subject, but no replies, especially about receiving. There exist example how to send attachment, but I didn't found how to receive it. Is there any solution on python for receiving attachments? I even agree to change my SOAP tool from suds to anything that will works.

Thank you in advance.

Arkady
  • 2,084
  • 3
  • 27
  • 48

1 Answers1

0

I solved it with suds.

def GetWithFile(self, client, servicename, modelthings):
    func = client.get_suds_func('Retrieve' + servicename)
    clientclass = func.clientclass({})
    SoapClient = clientclass(func.client, func.method)
    binding = func.method.binding.input
    soap_xml = binding.get_message(func.method, [modelthings], {})

    soap_xml.children[0].children[1].children[0].attributes.append(u'attachmentInfo="true"')
    soap_xml.children[0].children[1].children[0].attributes.append(u'attachmentData="true"')
    soap_xml.children[0].children[1].children[0].attributes.append(u'ignoreEmptyElements="true"')

    SoapClient.last_sent(soap_xml)
    plugins = PluginContainer(SoapClient.options.plugins)
    plugins.message.marshalled(envelope=soap_xml.root())
    if SoapClient.options.prettyxml:
        soap_xml = soap_xml.str()
    else:
        soap_xml = soap_xml.plain()
    soap_xml = soap_xml.encode('utf-8')
    plugins.message.sending(envelope=soap_xml)
    request = Request(SoapClient.location(), soap_xml)
    request.headers = SoapClient.headers()
    reply = SoapClient.options.transport.send(request)
    print(reply)

    Files = []
    boundary = self.find_substring(reply.headers['content-type'], 'boundary="', '"')
    if boundary is not "":
        list_of_data = reply.message.split(boundary)
        list_of_data.pop(0)
        list_of_data.pop(len(list_of_data) - 1)
        soap_body = '<SOAP-ENV:Envelope' + self.find_substring(list_of_data[0], '<SOAP-ENV:Envelope', '</SOAP-ENV:Envelope>') + '</SOAP-ENV:Envelope>'

        for line in list_of_data[1:]:
            File = SMFile()
            Files.append(File)
            File.filename = self.find_substring(line, 'Content-Location: ', '\r\n')
            File.key = self.find_substring(line, 'Content-ID: ', '\r\n')

            idx = line.index( 'Content-ID:' )
            start_idx = line.index( '\r\n\r\n' , idx ) + len('\r\n\r\n')
            fin_idx = line.rindex( '\r\n--', start_idx )
            File.body = line[start_idx: fin_idx]
            File.size = fin_idx - start_idx
    else:
        soap_body = '<SOAP-ENV:Envelope' + self.find_substring(reply.message, '<SOAP-ENV:Envelope', '</SOAP-ENV:Envelope>') + '</SOAP-ENV:Envelope>'

    ctx = plugins.message.received(reply=soap_body)
    soap_body = ctx.reply
    if SoapClient.options.retxml:
        answer = soap_body
    else:
        answer = SoapClient.succeeded(binding, soap_body)

    dict = {}
    self.FieldsToDict(answer.model.instance, dict)

    return {u'body': answer, u'Files': Files}

Here we extract some low level of suds, being able to fix any field in envelope. Then, after reply was got, we parse all boundaries and receive as many files, as we got.

Arkady
  • 2,084
  • 3
  • 27
  • 48
  • Hey Arkady, great that you took the time to come back and answer your own question! It sounds a bit cryptic to me though. Could you maybe illustrate your code with and example? Do you use this method "as is" or is it supposed to be embedded within suds itself? Thanks – jlengrand Feb 25 '16 at 14:35