How to send a file through Soap in python?

前端 未结 3 956
一个人的身影
一个人的身影 2020-12-10 22:36

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

相关标签:
3条回答
  • 2020-12-10 23:19

    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)
    
    0 讨论(0)
  • 2020-12-10 23:33

    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 讨论(0)
  • 2020-12-10 23:38

    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.

    0 讨论(0)
提交回复
热议问题