Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Flask-Python创建数据流_Python_Rest_Http_Flask_Stream - Fatal编程技术网

使用Flask-Python创建数据流

使用Flask-Python创建数据流,python,rest,http,flask,stream,Python,Rest,Http,Flask,Stream,我正在开发一个服务器,它使用Flask从服务器获取日志文件。 下面是获取日志的方法的一个片段: @app.route('/logs', methods=['POST']) def logs(): if request.method == 'POST': if request.headers['Content-Type'] == 'application/octet-stream': return not_implemented() i

我正在开发一个服务器,它使用
Flask
从服务器获取日志文件。 下面是获取日志的方法的一个片段:

@app.route('/logs', methods=['POST'])
def logs():
    if request.method == 'POST':
      if request.headers['Content-Type'] == 'application/octet-stream':
            return not_implemented()

        if request.headers['Content-Type'] == 'application/gzip':
            # @TODO: Implement me: I must return a gzip file
            return not_implemented()
我想要的是创建一个流,所以当我使用param
filename=foo.txt
/logs
发出
POST
请求时,我想要返回一个流,这样我就可以在客户端读取该文件。到目前为止,我尝试编写以下方法:

def get_stream(filename):
    """Return the file as a stream of binaries

    :return file: A file pointer for the binary file
    """
    chunk_size = 4096
    try:
        if os.path.exists(filename):
            with open(filename, "bw") as f:
                while True:
                    chunk = f.read(chunk_size)
                    #@TODO: Add the remaining of the implementation
    except Exception, ex:
        log.error(ex)
        return None
我不明白如何使用
yield
get\u stream()
方法返回迭代器。我知道我必须归还一些东西,比如:

return Response(get_stream("foo.txt"), mimetype="application/octet-stream")
有人能帮我吗