Получить вложение SOAP

#python-2.7 #suds

#python-2.7 #мыльная пена

Вопрос:

Есть много вопросов с той же темой, но ответов нет, особенно о получении. Существует пример, как отправить вложение, но я не нашел, как его получить. Есть ли какое-либо решение на python для получения вложений? Я даже согласен сменить свой инструмент SOAP с suds на все, что будет работать.

Заранее благодарю вас.

Ответ №1:

Я решил это с помощью 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: ', 'rn')
            File.key = self.find_substring(line, 'Content-ID: ', 'rn')

            idx = line.index( 'Content-ID:' )
            start_idx = line.index( 'rnrn' , idx )   len('rnrn')
            fin_idx = line.rindex( 'rn--', 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}
  

Здесь мы извлекаем некоторый низкий уровень пены, имея возможность исправить любое поле в конверте. Затем, после получения ответа, мы анализируем все границы и получаем столько файлов, сколько получили.

Комментарии:

1. Привет, Аркадий, здорово, что ты нашел время вернуться и ответить на свой собственный вопрос! Хотя для меня это звучит немного загадочно. Не могли бы вы проиллюстрировать свой код примером и? Используете ли вы этот метод «как есть» или он должен быть встроен в саму suds? Спасибо