Return media files (such as jpg, png) in Response body other than String

191 views
Skip to first unread message

Shu Lin

unread,
Dec 30, 2012, 9:13:39 PM12/30/12
to pylons...@googlegroups.com
Hi,

If I have other objects other than String, which is holding a jpg or png file, I like to return it as Response body, how can I do?

I tried a piece of code like this:

    mimetype = image/png
    body = fs.open(access_path, 'rb')

    return Response(body, content_type=mimetype)

Here "body" is an buffer object from a kind of filesystem which represents the content in the file. Since, it doesn't support len() like String. I can't passed the Response check for len(). I tried app_iter also like this:

    return Response(app_iter=[body], content_type=mimetype)

But, if I did like this, the wsgiref will complain about the body is not String type. 

===========

2012-12-29 23:33:59,521 INFO  [fs.tahoelafs][MainThread] (4363489744) Opening file /family/Ada/IMG_0355.JPG in mode rb

Traceback (most recent call last):

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run

    self.finish_response()

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response

    self.write(data)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 202, in write

    assert type(data) is StringType,"write() argument must be string"

AssertionError: write() argument must be string

==========

Is there anyway to work around this?

Thanks a lot in advance!

-Shu

Gael Pasgrimaud

unread,
Dec 30, 2012, 9:31:38 PM12/30/12
to pylons...@googlegroups.com
Hi,

Le 31/12/2012 03:13, Shu Lin a �crit :
You can use wsgiref's FileWrapper:
http://docs.python.org/2/library/wsgiref.html#wsgiref.util.FileWrapper

>>> resp.app_iter=FileWrapper(open(path, 'rb'))

You can also have a look at wsgithumb:
https://github.com/gawel/wsgithumb/blob/master/wsgithumb/utils.py#L126

You can just use it. Or at least get some responses for your problem by
reading the code.

>
> Thanks a lot in advance!
>
> -Shu
>
> --
> You received this message because you are subscribed to the Google
> Groups "pylons-devel" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-devel/-/uxwTx2y06D0J.
> To post to this group, send email to pylons...@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-devel...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-devel?hl=en.

Chris McDonough

unread,
Dec 30, 2012, 9:56:02 PM12/30/12
to pylons...@googlegroups.com
<http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/static_assets/files.html#serving-file-content-dynamically>

Instead of just using an open file as the app_iter, you can use a
pyramid.response.FileIter object. This will chunk the file out in
fixed-sized blocks:

http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/api/response.html#pyramid.response.FileIter

Or you can just shortcut the whole business and return a FileResponse
instead of a response:

http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/api/response.html#pyramid.response.FileResponse


- C


Shu Lin

unread,
Dec 30, 2012, 11:51:12 PM12/30/12
to pylons...@googlegroups.com
I figured it out the reason is the package I used, pyfilesystem, http://code.google.com/p/pyfilesystem/

I used the RemoteFileBuffer. Now, I use RemoteFileBuffer._read() to get out the local buffer object which makes the Response.body happy.

Thanks a lot for all the helps!
-Shu
Reply all
Reply to author
Forward
0 new messages