Search code examples
restfilecurlpython-3.6tornado

Send file using Tornado in Python-3.6


I am trying to send back a file using REST GET with Tornado but the checksum of the returned file is different every time. What could the reason be behind this? Are the chunks returned in incorrect order?

I am using curl to download the file.

Thanks for any advice! :-)

async def get(self, filename):
    chunkSize = 1024 * 1024 * 1 # 1 Mib
    with open(filename, 'rb') as f:
        while True:
            chunk = f.read(chunkSize)
            if not chunk:
                break
            try:
                self.write(chunk) # write the chunk to the response
                await self.flush()# send the chunk to the client
            except iostream.StreamClosedError:
                break
            finally:
                del chunk
                await gen.sleep(0.000000001)
    self.finish()

Edit: I tried downloading a local file and found that curl status is added to the beginning of the file.

curl --user test -i -X GET http://localhost:8085/download/testfile.dat --output testfile.dat

Works much better with wget which does not add connection.

wget --http-user=test --http-passwd=test http://localhost:8085/download/testfile.dat


Solution

  • Edit: I tried downloading a local file and found that curl status is added to the beginning of the file.

    curl --user test -i -X GET http://localhost:8085/download/testfile.dat --output testfile.dat
    

    That's what curl -i does. From the man page:

           -i, --include
                  Include the HTTP response headers in the output. The HTTP
                  response headers can include things like server name,
                  cookies, date of the document, HTTP version and more...
    
                  To view the request headers, consider the -v, --verbose
                  option.
    

    Remove -i from your curl command line and it should work like your wget command line.