获取SOAP附件

时间:2014-06-20 14:14:49

标签: python-2.7 suds

关于同一主题有很多问题,但没有回复,特别是关于接收。存在如何send attachment的示例,但我没有找到如何接收它。 在python上有接收附件的解决方案吗?我甚至同意将我的SOAP工具从 suds 改为任何可行的。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我用泡沫解决了它。

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}

在这里,我们提取一些低水平的泡沫,能够修复信封中的任何字段。然后,在得到答复后,我们解析所有边界并接收尽可能多的文件。