Hi,

Le 31/12/2012 03:13, Shu Lin a écrit :
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?

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-devel@googlegroups.com.
To unsubscribe from this group, send email to
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/pylons-devel?hl=en.

--
You received this message because you are subscribed to the Google Groups 
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-devel?hl=en.

Reply via email to