Buongiorno a tutti,<div><br></div><div>è ormai un certo periodo di tempo che utilizzo "Suds" come client soap per python, trovandomi molto bene e avendo pochi problemi!</div><div>Sarei adesso interessato ad utilizzare SOAP with attachment, mio malgrado ho scoperto che Suds non supporta, in modo esplicito, quest'estensione.</div>
<div>Non perdendomi d'animo ho trovato una discussione su stackoverflow in cui si parla proprio di questo problema:</div><div><br></div><div><a href="http://stackoverflow.com/questions/6601107/how-to-send-a-file-through-soap-in-python">http://stackoverflow.com/questions/6601107/how-to-send-a-file-through-soap-in-python</a></div>
<div><div><br></div><div>Un tizio ha scritto una sorta di wrapper in grado di gestire i file allegati ( <a href="https://d-feet.fedorahosted.org/suds/attachment/ticket/350/soap_attachments.2.py">https://d-feet.fedorahosted.org/suds/attachment/ticket/350/soap_attachments.2.py</a> ).</div>
<div>Facendo una prova, ho da subito riscontrato un problema in merito alla riga 75 del sopracitato link, client.location() non è un metodo valido, ho però sostituito quest'istruzione con client.options.location, che sembra essere ciò che si vuole ottenere da quel fantomatico metodo..</div>
<div><br></div><div>In seguito ho testato un servizio di esempio ( <a href="http://soapclient.com/xml/soapresponder.wsdl">http://soapclient.com/xml/soapresponder.wsdl</a> ), non ottenendo errori ma ottenendo una risposta non attesa: il wsdl stesso..</div>
<div><br></div><div>Chiedo, se qualcuno si è già addentrato in queste problematiche, gentilmente un piccolo aiuto per capire dove ho sbagliato o meno!</div><div><br></div><div>mi permetto di postare di seguito il codice che ho usato, grazie anticipatamente a tutti!</div>
<div><br></div><div><div>#<a href="http://stackoverflow.com/questions/6601107/how-to-send-a-file-through-soap-in-python">http://stackoverflow.com/questions/6601107/how-to-send-a-file-through-soap-in-python</a></div><div>import re</div>
<div>import uuid</div><div>import mimetypes</div><div>from suds.client import Client</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div>def with_soap_attachment(suds_method, attachment_data, *args, **kwargs):</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> """ Add an attachment to a suds soap request.</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> attachment_data is assumed to contain a list:</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> ( <attachment content>, <content id>, <mime-type> )</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span> The attachment content is only required list element.</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> """</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span> </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> from suds.transport import Request</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> # Suds doesn't currently support SOAP Attachments, so we have to build our</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> # own attachment support, using parts of the suds library</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> MIME_DEFAULT = 'text/plain'</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> attachment_transfer_encoding = '8bit'</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> soap_method = suds_method.method</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> if len(attachment_data) == 3:</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> data, attachment_id, attachment_mimetype = attachment_data</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> elif len(attachment_data) == 2:</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> data, attachment_id = attachment_data</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> attachment_mimetype = MIME_DEFAULT</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> elif len(attachment_data) == 1:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> data = attachment_data</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> attachment_mimetype = MIME_DEFAULT</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> attachment_id = uuid.uuid4()</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> # Generate SOAP XML appropriate for this request</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> soap_client = suds_method.clientclass(kwargs)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> binding = soap_method.binding.input</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> soap_xml = binding.get_message(soap_method, args, kwargs)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> # Prepare MIME headers & boundaries</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> boundary_id = 'uuid:%s' % uuid.uuid4()</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> root_part_id ='uuid:%s' % uuid.uuid4()</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> request_headers = {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 'Content-Type': '; '.join([</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span> 'multipart/related',</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 'type="text/xml"',</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> 'start="<%s>"' % root_part_id,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 'boundary="%s"' % boundary_id,</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> ]),</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> }</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> soap_headers = '\n'.join([</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> 'Content-Type: text/xml; charset=UTF-8',</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 'Content-Transfer-Encoding: 8bit',</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> 'Content-Id: <%s>' % root_part_id,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> '',</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> ])</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> attachment_headers = '\n'.join([</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 'Content-Type: %s' % attachment_mimetype,</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> 'Content-Transfer-Encoding: %s' % attachment_transfer_encoding,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 'Content-Id: <%s>' % attachment_id,</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> '',</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> ])</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> # Build the full request</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> request_text = '\n'.join([</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> '',</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> '--%s' % boundary_id,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> soap_headers,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> str(soap_xml),</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> '--%s' % boundary_id,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> attachment_headers,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> data,</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> '--%s--' % boundary_id</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> ])</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> # Stuff everything into a request object</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> headers = suds_method.client.options.headers.copy()</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> headers.update(request_headers)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> request = Request(suds_method.client.options.location, request_text)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> #print str(request_text)+'****'</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> request.headers = headers</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span> #print str(request.headers)+'****'</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> # Send the request</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> response = suds_method.client.options.transport.send(request)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> return response</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> </div><div><br></div><div>if __name__ == '__main__':</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>infile = 'C:/Documents and Settings/filo/Desktop/test_soap_with_attachment.py'</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>f = open(infile,'rb')</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>data_file = f.read().encode("base64")</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>data_file_type = mimetypes.guess_type(infile)[0]</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>(filename,ext) = infile.split('.')</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>url = "<a href="http://soapclient.com/xml/soapresponder.wsdl">http://soapclient.com/xml/soapresponder.wsdl</a>"</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>cl = Client(url,location=url)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>identifier = with_soap_attachment(cl.service.Method1, [data_file, '1', data_file_type],"ciao","ciao")</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>print identifier.message</div></div>-- </div><div><br><div>FF</div><div><br></div><br>
</div>