문제

I created a simple threaded python server, and I have two parameters for format, one is JSON (return string data) and the other is zip. When a user selects the format=zip as one of the input parameters, I need the server to return a zip file back to the user. How should I return a file to a user on a do_GET() for my server? Do I just return the URL where the file can be downloaded or can I send the file back to the user directly? If option two is possible, how do I do this?

Thank you

도움이 되었습니까?

해결책 2

The issue was that I hadn't closed the zipfile object before I tried to return it. It appeared there was a lock on the file.

To return a zip file from a simple http python server using GET, you need to do the following:

  1. Set the header to 'application/zip'

    self.send_header("Content-type:", "application/zip")

  2. Create the zip file using zipfile module

  3. Using the file path (ex: c:/temp/zipfile.zip) open the file using 'rb' method to read the binary information

    openObj = open( < path > , 'rb')

  4. return the object back to the browser

    openObj.close() del openObj self.wfile.write(openObj.read())

    That's about it. Thank you all for your help.

다른 팁

You should send the file back to the user directly, and add a Content-Type header with the correct media type, such as application/zip.

So the header could look like this:

Content-Type: application/zip
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top