2

I am writing an application where receive file like a stream like this:

zipfile = subprocess.Popen(somearguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=in_stream, close_fds=True)

return  zipfile.stdout.read()

How I can convert this stream to zip file, and send it to browser to download? I have read a lot of articles, but I am new in python and do not know what will be the best solution. If file is correct I will display download link:

<a href=”test.py”> "Click for download" </a> 

which will redirect to test.py script.

where test.py has to receive this zip file and send it to browser for download. I have read about context-disposition and how it should be used in such cases but still do not have solution. Thank you in advance!

Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
user2739823
  • 397
  • 1
  • 7
  • 24
  • When are you doing the `subprocess` call? Is this something that would be done ahead of time, or only when the file is requested by a web client? Are you using any web framework yet? – Blckknght Sep 07 '14 at 15:00
  • Subprocess call is invoked in every 3 minutes. It executes back end command which returns zip file as a stream. However for me it is not so important, how to convert this stream to file, but how to send this file to test.py script and make it downloadable through browser. – user2739823 Sep 07 '14 at 15:11
  • What is it exactly you want to know? How to tell the script which file to compress? How to compress it? Side note: Are you using a web framework for this? Like Django? (in that case the link would look totally different) – Augusto G Sep 07 '14 at 15:22
  • I want to know how user will be able to click on lnik "Download" and this will open new window where file will start to download. Sorry if I do not express myself clear enough. I do not use Django, for client side only html, css ... back end - python. – user2739823 Sep 07 '14 at 15:31
  • If your web site is pure HTML/CSS, it might make sense to just write the zip-file data to a regular file somewhere your web server can see it. Then you don't need a special Python script to serve it up, just have the web page point directly at the file. – Blckknght Sep 07 '14 at 16:03
  • http://stackoverflow.com/questions/18339745/how-to-deploy-zip-files-or-other-binaries-trough-cgi-in-python this is what I am looking for. I will try with this approach to send the file to browser for download. Thank you for your comments. I will update my post when I find a proper solution based on your advices. – user2739823 Sep 07 '14 at 16:15

0 Answers0