How to send a file via Soap in python?

I want to send a zip file via SOAP (from a SOAP client to a SOAP server) in python.

After reading this SO question , I choose to use suds as my python client. But according to this , foam does not support sending attachments. A method is given to get around the problem, but I could not get it to work. I am puzzled by what I should give as parameters.

Does anyone know how to send a file via Soap in python?

If necessary, I will switch to another SOAP client library.

+1
source share
3 answers

Download the provided shell, and then where you usually say something like ...

client.service.fooMethod(fooParam1,fooParam2,...)

... instead of this...

soap_attachments.with_soap_attachment(client.service.fooMethod,binaryParam,fooParam1,fooParam2,...)

binaryParam , soap_attachements.py. , png-, ( ):

imageFile = open('imageFile.png','rb')
imageData = imageFile.read()
mimeType = 'image/png'
binaryParam = (imageData, uuid.uuid4(), mimeType)
+6

- SOAP. - , SOAP, Base64 SOAP- . , . ? FTP, WebDAV .

+3

I made the following changes to soap_attachments.py under singing to make my own downloads work. You may not need some of the changes I made for this, but hopefully it will at least give you a start.

--- /home/craig/Downloads/soap_attachments.py   2011-07-08 20:38:55.708038918 -0400
+++ soap_attachments.py 2011-06-21 10:29:50.090243052 -0400
@@ -1,4 +1,8 @@
+import uuid
+import re
 def with_soap_attachment(suds_method, attachment_data, *args, **kwargs):
+    HUD_ARM_SERVICE_URL = suds_method.client.wsdl.url
+    HUD_ARM_SERVICE_URL = HUD_ARM_SERVICE_URL.replace('wsdl','xsd')
     """ Add an attachment to a suds soap request.

     attachment_data is assumed to contain a list:
@@ -16,7 +20,9 @@
     soap_method = suds_method.method

     if len(attachment_data) == 3:
+        print "here"
         data, attachment_id, attachment_mimetype = attachment_data
+        attachment_id = uuid.uuid4()
     elif len(attachment_data) == 2:
         data, attachment_id = attachment_data
         attachment_mimetype = MIME_DEFAULT
@@ -55,7 +61,7 @@
     ])

     # Build the full request
-    request_text = '\n'.join([
+    request_text = '\r\n'.join([
       '',
       '--%s' % boundary_id,
       soap_headers,

Then I use:

f = open(dir_path + infile,'rb')
data_file = f.read()
data_file_type = mimetypes.guess_type(infile)[0]
(filename,ext) = infile.split('.')
...

clientargs = [...]
identifier = with_soap_attachment(client.service.fooThing, [data_file, '1', data_file_type], credentials['foo'],credentials['bar'], morefoo)

You may not need all these changes, but this is what made me move.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1702413/


All Articles