String Literal to Blob
Steve Holden
steve at holdenweb.com
Thu Apr 10 12:58:49 EDT 2008
Victor Subervi wrote:
> 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 <http://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 <http://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
>
Of course it prints garbage. Since you claim to understand HTML I have
no idea what makes you expect that it would print anything else. That's
because you are sending garbage to the browser instead of the HTML your
content type promises.
THE VALUE OF THE IMG TAG'S SRC ATTRIBUTE SHOULD BE THE URL OF AN IMAGE<
NOT THE IMAGE ITSELF. Sorry, I don't normally shout like that. So pay
attention when I do.
First let's agree that what you are writing here is a web script to
return an image, NOT a web page with an embedded image. So ...
Now you formulate a correct response instead. You should NOT be
returning a content type of text/html here, you should be returning a
content type of image/jpeg. So your code should read
#! /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
If you then direct your browser to the correct URL to access the output
of this script you will see your image.
When you want to see your image in the context of some HTML, write
*another page* and you put the URL of the image inside it like this:
<img src="imageURL">
as a reference to the image your script returns.
I think you are on your own from here. If you follow instructions you
should not need any further help.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list