String Literal to Blob

Victor Subervi victorsubervi at gmail.com
Thu Apr 10 10:46:11 EDT 2008


Okay, here is where we find the fly in the ointment. If I run this code:

#! /usr/bin/python
import MySQLdb
print "Content-type: image/jpeg\r\n"
host = 'mysqldb2.ehost-services.com'
db = 'benobeno_bre'
user = 'benobeno'
passwd = '21122112'
connection = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
cursor = connection.cursor()
cursor.execute('select pic1 from products where id="2";')
content = cursor.fetchall()[0][0]
content = content.tostring()
print content
f = open("2.jpg", "w")
f.write(content)
f.close()
all is well :) If, however, I change two lines to make it an html page:

#! /usr/bin/python
import MySQLdb
# print "Content-type: image/jpeg\r\n"
print "Content-type: text/html\n"
host = 'mysqldb2.ehost-services.com'
db = 'benobeno_bre'
user = 'benobeno'
passwd = '21122112'
connection = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
cursor = connection.cursor()
cursor.execute('select pic1 from products where id="2";')
content = cursor.fetchall()[0][0]
content = content.tostring()
print '<img src="%s"><br /><br />' % content
# print content
f = open("2.jpg", "w")
f.write(content)
f.close()
it prints garbage. It does not yield the image. Now, what?
TIA.
Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080410/542b5cbe/attachment.html>


More information about the Python-list mailing list