【问题标题】:Send file using Tornado in Python-3.6在 Python-3.6 中使用 Tornado 发送文件
【发布时间】:2021-06-07 12:04:58
【问题描述】:

我正在尝试使用带有 Tornado 的 REST GET 发回一个文件,但每次返回的文件的校验和都不同。 这背后的原因可能是什么?块返回的顺序是否错误?

我正在使用 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状态。

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 file curl python-3.6 tornado


    【解决方案1】:

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

    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 命令行一样工作。

    【讨论】:

    • 谢谢!我想我将 -k 与 -i 混合在一起表示不安全
    【解决方案2】:

    代码中的问题是我向 REST 客户端写入了一些额外的数据,这些数据最终出现在下载的文件中。我还发现 curl 在下载的文件中添加了一些 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-20
      • 2010-12-06
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      相关资源
      最近更新 更多