Problem Displaying Pics

Rami Chowdhury rami.chowdhury at gmail.com
Wed Oct 7 16:52:30 EDT 2009


On Wed, 07 Oct 2009 13:24:28 -0700, Victor Subervi  
<victorsubervi at gmail.com> wrote:

> I did that. In fact, just to make things easier, I wrote out exactly  
> what is
> supposed to be rendered, as below:
>
> #!/usr/local/bin/python
> import cgitb; cgitb.enable()
> import MySQLdb
> import cgi
> import sys,os
> sys.path.append(os.getcwd())
> from login import login
> user, passwd, db, host = login()
> form = cgi.FieldStorage()
> db = MySQLdb.connect(host, user, passwd, db)
> cursor= db.cursor()
> sql = "select pic1 from products where id='1';"
> cursor.execute(sql)
> content = cursor.fetchall()[0][0].tostring()
> cursor.close()
> print '''Content-Type: image/jpeg'''	# Only print one line
>
> Content-Encoding: base64
> '''
> print
> print content.encode('base64')
>

Just to reiterate what Gabriel and Carsten have pointed out, I'd suggest  
changing the last few lines of the script, as the comments below indicate:

print '''Content-Type: image/jpeg''' # One header line
print '''Content-Encoding: base64''' # Another header line. Note *no*  
blank line between them
print 				     # Blank line signals the end of the headers
print content.encode(base64)	     # Base64-encoded content comes *after*  
the blank line

If you include extra blank lines after the headers, the browser will  
misunderstand where the binary image data begins, and so will see a  
malformed JPEG.

If this doesn't work, I suggest changing the content-type header to  
text/plain and trying to manually decode and check the image data to  
ensure it's valid.

>
> On Wed, Oct 7, 2009 at 2:47 PM, Gabriel Genellina  
> <gagsl-py2 at yahoo.com.ar>wrote:
>
>> En Wed, 07 Oct 2009 12:00:13 -0300, Victor Subervi
>> <victorsubervi at gmail.com> escribió:
>>
>> > print '''Content-Type: image/jpeg
>> >
>> > '''
>> > print
>> > print content
>>
>> On Wed, Oct 7, 2009 at 9:51 AM, Gabriel Genellina <gagsl-
>> py2 at yahoo.com.ar>wrote:
>>
>> > That's still wrong. The output should be:
>> >
>> > - a line containing Content-Type: image/jpeg
>> > - a blank line (no more!)
>>         ^^^^^^^^^^^^^^^^^^^^^^^^^
>> > - the actual image data
>>
>> --
>> Gabriel Genellina
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>



-- 
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



More information about the Python-list mailing list