在 Python-3.6 中使用 Tornado 发送文件

Send file using Tornado in Python-3.6

我正在尝试使用 REST GET 和 Tornado 发回文件,但返回文件的校验和每次都不同。 这背后的原因是什么?块返回的顺序不正确吗?

我正在使用 curl 下载文件。

感谢任何建议! :-)

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()

编辑: 我尝试下载本地文件,发现文件开头添加了curl status。

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

使用不添加连接的 wget 效果更好。

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

代码中的问题是我向 REST 客户端写入了一些额外的数据,这些数据最终出现在下载的文件中。我还发现 curl 在下载的文件中添加了一些额外的 headers 而 wget 不会。 尝试使用 -s 和 --silent 但这没有帮助。以下数据已添加到文件的开头。

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

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

这就是 curl -i 所做的。来自手册页:

       -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.

从您的 curl 命令行中删除 -i,它应该像您的 wget 命令行一样工作。