Re: [flask] Big passthrough download without response object?
- From:
- Marten Lehmann
- Date:
- 2010-11-11 @ 14:58
Hello,
> I think request will accept anything iteratable, so you can pass it an
> open file:
>
> from flask import Flask,Response
>
> app=Flask(__name__)
>
> @app.route('/')
> def sendfile():
> return Response(open('staticfile.py','r'), content_type='text/plain' )
>
> app.run()
this looks reasonable (and other examples as well). The special
situation in my case is, that I'm working with temporary files. That
means, I'm creating them on the fly, then the content shall be sent to
the client and afterwards the file shall be deleted. If the client makes
another request, the file will be newly created.
My machine has 8 GB of RAM and the temporary files are at a maximum of
1GB each, mostly even less then 100 MB. So if it's not possible for the
function within the flask app to send the content step by step, but only
all at once, then probably reading everything into memory and returning
it (return f.read()) seems to be the only option here. Or any other idea?
Kind regards
Marten
Re: [flask] Big passthrough download without response object?
- From:
- Robert Mela
- Date:
- 2010-11-14 @ 02:10
could not decode message
Re: [flask] Big passthrough download without response object?
- From:
- Robert Mela
- Date:
- 2010-11-17 @ 15:21
could not decode message
Re: [flask] Big passthrough download without response object?
- From:
- Simon Sapin
- Date:
- 2010-11-11 @ 23:36
Le 11/11/2010 23:58, Marten Lehmann a écrit :
> this looks reasonable (and other examples as well). The special
> situation in my case is, that I'm working with temporary files. That
> means, I'm creating them on the fly, then the content shall be sent to
> the client and afterwards the file shall be deleted. If the client makes
> another request, the file will be newly created.
>
> My machine has 8 GB of RAM and the temporary files are at a maximum of
> 1GB each, mostly even less then 100 MB. So if it's not possible for the
> function within the flask app to send the content step by step, but only
> all at once, then probably reading everything into memory and returning
> it (return f.read()) seems to be the only option here. Or any other idea?
Hi,
If you temporary file is on the filesystem or a file-like object, see
flask.send_file:
http://flask.pocoo.org/docs/api/#flask.send_file
Otherwise, you need something like this:
http://flask.pocoo.org/mailinglist/archive/2010/11/3/using-yield/#478b0c1829b5263700da1db7d2d22c79
Regards,
--
Simon Sapin
Re: [flask] Big passthrough download without response object?
- From:
- Zahari Petkov
- Date:
- 2010-11-11 @ 15:12
On Thu, Nov 11, 2010 at 4:58 PM, Marten Lehmann <lehmann@cnm.de> wrote:
> Hello,
>
>> I think request will accept anything iteratable, so you can pass it an
>> open file:
>>
>> from flask import Flask,Response
>>
>> app=Flask(__name__)
>>
>> @app.route('/')
>> def sendfile():
>> return Response(open('staticfile.py','r'), content_type='text/plain' )
>>
>> app.run()
>
> this looks reasonable (and other examples as well). The special
> situation in my case is, that I'm working with temporary files. That
> means, I'm creating them on the fly, then the content shall be sent to
> the client and afterwards the file shall be deleted. If the client makes
> another request, the file will be newly created.
>
> My machine has 8 GB of RAM and the temporary files are at a maximum of
> 1GB each, mostly even less then 100 MB. So if it's not possible for the
> function within the flask app to send the content step by step, but only
> all at once, then probably reading everything into memory and returning
> it (return f.read()) seems to be the only option here. Or any other idea?
>
> Kind regards
> Marten
>
Just a quick idea, not sure whether it will work for you, but
something to keep in mind is that you may delegate such responsibility
to your HTTP server:
http://wiki.nginx.org/XSendfile
Thanks,
Zahari
Re: [flask] Big passthrough download without response object?
- From:
- Robert Mela
- Date:
- 2010-11-06 @ 02:23
IIRC, you can also pass a named "headers" parameter to the request for
any arbitrary headers.
return Request( open("foo.txt", "r", headers={
'Content-type':'text/plain' } )
Also check out the doc on werkzeug Headers at
http://werkzeug.pocoo.org/documentation/dev/datastructures.html#http-related
It uses Content-disposition as one of the example headers.
from flask import Flask,Response
from werkzeug import Headers
app=Flask(__name__)
@app.route('/')
def sendfile():
h=Headers()
h.add( 'Content-type', 'text/plain', charset='utf8' )
h.add( 'Content-disposition', 'attachment',
filename='foo.txt' )
return Response(open('myfile.txt','r'), headers=h )
app.run(debug=True)
And also you can use the headers object, which is kinda neat in how it
allows adding headers with parameters
headers=Header
On 11/5/10 10:11 PM, Robert Mela wrote:
> I think request will accept anything iteratable, so you can pass it an
> open file:
>
> from flask import Flask,Response
>
> app=Flask(__name__)
>
> @app.route('/')
> def sendfile():
> return Response(open('staticfile.py','r'),
> content_type='text/plain' )
>
> app.run()
>
> On 11/5/10 8:10 PM, Marten Lehmann wrote:
>> Well,
>>
>> On 06.11.2010 00:56, Thadeus Burgess wrote:
>>> from flask import send_static_file
>> but how do I specify special headers like content-type and
>> content-disposition here?
>>
>> Kind regards
>> Marten
>>
>