pdf file can't be send to suds(client) using ladon with SOAP protocol

Asked by bibin kk on 2012-10-11

Hi, i am using Ladon and Python under mod_wsgi working with Apache2 with SOAP protocol.
I have got the following traceback, when I tried to get a file from specified location in server with the service "download"

 Traceback (most recent call last):
   File "/var/www/Accounting_Engine/src/modules/openingBalance/wdwOpeningBalance.py", line 283, in on_preview_button_activate
     lst_ins_file_response = self.ae.service.download(names = lst1)
   File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py", line 542, in __call__
   File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py", line 602, in invoke
   File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py", line 649, in send
   File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py", line 702, in failed
   File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/bindings/binding.py", line 258, in get_fault
   File "/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/sax/parser.py", line 136, in parse
   File "/usr/lib/python2.6/xml/sax/expatreader.py", line 107, in parse
     xmlreader.IncrementalParser.parse(self, source)
   File "/usr/lib/python2.6/xml/sax/xmlreader.py", line 123, in parse
     self.feed(buffer)
   File "/usr/lib/python2.6/xml/sax/expatreader.py", line 211, in feed
     self._err_handler.fatalError(exc)
   File "/usr/lib/python2.6/xml/sax/handler.py", line 38, in fatalError
     raise exception
 xml.sax._exceptions.SAXParseException: <unknown>:1:0: syntax error

The service 'download' is like as follows:
 server apache2
 --------------
 # -*- coding: utf-8 -*-
 from ladon.types.attachment import attachment
 from ladon.compat import PORTABLE_STRING
 from os.path import dirname,abspath,join
 upload_dir = join(dirname(abspath(__file__)),'pdfDocuments')

 class File(LadonType):
  data = attachment
  name = PORTABLE_STRING

 class AccountEngine(object):
     @ladonize([PORTABLE_STRING], rtype=[File])
     def download(self,names,**args):
  """
  Download multiple files at once. For each name in the <b>names</b> the service
  attempts to find a file in service/upload that matches it. If a name does not
  have a matching file it is ignored.

  @param names: A list of the file names
  @rtype: Returns a list of File objects
  """
  global upload_dir
  print upload_dir
  response = []
  for name in names:
          f = File()
          f.name = name
          f.data = attachment(open(join(upload_dir,name),'rb'))
          response += [f]
  return response

I think the attachment is not/less compatible with SOAP
Anybody knows how to solve this?

This is how I call the service download (with a list of file names as the parameter):
 client python
 -------------
        lst1 = ["__File__.pdf"]
        ins_file_response = self.ae.service.download(names = lst1)

Question information

Language:
English Edit question
Status:
Solved
For:
ladon Edit question
Assignee:
No assignee Edit question
Solved by:
bibin kk
Solved:
2012-10-19
Last query:
2012-10-19
Last reply:
2012-10-12
jsgaarde (jakob-simon-gaarde) said : #1

Hi Bibin

Please read this article: http://ladonize.org/index.php/Ladon_Attachments

In short SOAP with attachments is very poorly designed with no flexibility, JSONWSP which is a much more modern protocol can handle dynamic attachments.

Best regards
Jakob Simon-Gaarde

bibin kk (kkbkk007) said : #2

Hi jakob, Thank you for trying to help. I read that article. Should I have to change SOAP to JSON-WSP?. Is their any other solution.

jsgaarde (jakob-simon-gaarde) said : #3

Hi Bibin.
I would prefer JSON-WSP over SOAP anytime unless I have a client that simply can't communicate in that protocol.

If you need to use SOAP take a look at this:
https://answers.launchpad.net/ladon/+question/185564

Best regards

Jakob

bibin kk (kkbkk007) said : #4

H jakob,
I think I got a solution to solve my problem using SOAP ,

[link]: https://fedorahosted.org/suds/attachment/ticket/350/soap_attachments.2.py

(But now the problem is that I have no idea about theose arguments passed in method "with_soap_attachment" in the above link.)
Hopes it will help.

bibin kk (kkbkk007) said : #5

And Thank you jakob, for the previous link, :-)
I checked it. But I still wants to try some more time on SOAP. The 4th comment in that link was partially helpful. ("Partially" because, I need the download service. upload does not need a return value ;-) )

jsgaarde (jakob-simon-gaarde) said : #6

Uploading (request attachments)
This one i think we have already covered with this one: https://answers.launchpad.net/ladon/+question/185564

Downloading (response attachments etc.)
If you want to post an attachment with your SOAP response, you can add them manually using the "response_attachments" WSGI environment variable. So if you have a ladonized method like this one:

# -*- coding: utf-8 -*-
from ladon.types.attachment import attachment
from ladon.compat import PORTABLE_STRING
from ladon.ladonizer import ladonize

@ladonize(PORTABLE_STRING,rtype=int)
def download(self,filename,**kw):
  kw['response_attachments'].add_attachment(attachment(open('somefile','rb')))
  return 1

If what you want is to send a SOAP request and then turn the response into a HTTP download response look here:
http://ladonize.org/index.php/Python_CustomResponse

bibin kk (kkbkk007) said : #7

I soved the file transfer by using httplib.