开发者

How to send a file through Soap in python?

开发者 https://www.devze.com 2023-03-18 00:10 出处:网络
I want to send a zip file through SOAP (from a SOAP client to a SOAP server) in python. Following the reading of this SO question, I choose to use suds as my python client.

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

Following the reading of this SO question, I choose to use suds as my python client. But according to this, suds do not support sending attachment. A method is given to circumvent the problem but I've not been able to make it work. I'm puzzled over what I'm supposed to give as parameters.

Anyone know how to send a file through Soap in python ?

If needed 开发者_StackOverflow社区I'll switch to another SOAP client library.


Download the provided wrapper, and then where you would normally say something like...

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

...instead do...

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

Where binaryParam is of the type expected by soap_attachements.py. For example, if you wanted to send a png image I think (never done this) you would do:

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


Attachments are best way to send binary file through SOAP. If you can't use any other method but only SOAP, just encode your binaries with Base64 and paste it into SOAP method as a parameter. It isn't pure, but works great with small attachments. Large binaries? Use FTP, WebDAV and all others native ways for sending files between hosts.


I made the following changes to soap_attachments.py under suds to get my own uploads to work. You may not need some of the changes that I've made to this, but hopefully it'll 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,

I then 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 might not need all of these changes, but it's what got me going.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消