user1728363
user1728363

Reputation: 369

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

Upvotes: 0

Views: 518

Answers (2)

Ben Darnell
Ben Darnell

Reputation: 22134

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.

Upvotes: 2

user1728363
user1728363

Reputation: 369

The problem in the code was that I wrote some extra data to the REST client which ended up in the downloaded file. I also found that curl adds some extra headers in the downloaded file which wget does not do. Tried with -s and --silent but that did not help. Below data was added to the start of the file.

HTTP/1.1 200 OK
Server: TornadoServer/4.4.2
Content-Type: text/html; charset=UTF-8
Date: Mon, 07 Jun 2021 14:41:53 GMT
Transfer-Encoding: chunked

Upvotes: 0

Related Questions