String Literal to Blob

Steve Holden steve at holdenweb.com
Wed Apr 9 11:10:47 EDT 2008


Victor Subervi wrote:
> On Wed, Apr 9, 2008 at 10:24 AM, Steve Holden <steve at holdenweb.com 
> <mailto:steve at holdenweb.com>> wrote:
> 
>     Victor Subervi wrote:
>      > On Wed, Apr 9, 2008 at 1:14 AM, Gabriel Genellina
>      > <gagsl-py2 at yahoo.com.ar <mailto:gagsl-py2 at yahoo.com.ar>
>     <mailto:gagsl-py2 at yahoo.com.ar <mailto:gagsl-py2 at yahoo.com.ar>>> wrote:
> 
>  
> 
>     Now all you have to do is what I told you in the first place, which is
>     to remove the print statements before and after "print content".
> 
> 
> That is what I figured after I sent the email. However, I had tried that 
> with a test script and the working script, with bad results. Here is the 
> test script that should have worked flawlessly:
> 
> #! /usr/bin/python
> 
> import MySQLdb
> 
> print "Content-type: image/jpeg\r\n"
> host = 'host'
> db = 'bre'
> user = 'user'
> passwd = 'pass'
> connection = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
> cursor = connection.cursor()
> cursor.execute('select img from photo where id="7";')
> content = cursor.fetchall()

Bzzt! Nope. Cursor.fetchall() returns a list (length 1) or tuples 
(length 1) here, so this should be

content = cursor.fetchall()[0][0]

or, perhaps better

content = cursor.fetchone()[0]

> print "Content-Type: image/jpeg\nContent-Length: %d\n" % len(content)
> print content
> connection.commit()

The line above is completely unnecessary if the database has not been 
changed.

> connection.close()
> 
> This prints out the URL as an image! No idea why. But it does not 
> produce the desired image.
> 
I've no idea why either, but try fixing the script and see what that does.

> The following is the heart of the script for display. I tried entering 
> the Content-type where indicated, but it produces everything up to the 
> image, then produces the result of code from the exception...
> 
>     cursor.execute('select id from products where category="' + category 
> + '" order by sort_factor desc, price desc;')
>     ids = cursor.fetchall()
>     if len(ids[0]) != 0:
>       for id in ids:
>         for d in id:
>           print '<tr>\n'
>           cursor.execute('select * from products where id = ' + str(d) + 
> ';')
>           col_fields = cursor.fetchall()
>           if lang == 'es':
>             print '<b>ID: </b>', col_fields[0][0], '<br />'
>             print '<b>Nombre: </b>', col_fields[0][2], '<br />'
>             print '<b>Título: </b>', col_fields[0][6], '<br />'
>             print '<b>Descripción: </b>', col_fields[0][9], '<br />'
>             print '<b>Precio: </b>', col_fields[0][11], '<br />'
>             print '<b>Recámaras: </b>', col_fields[0][12], '<br />'
>             print '<b>Baños: </b>', col_fields[0][13], '<br />'
>             content = col_fields[0][14].tostring()
>             print "Content-Type: image/jpeg\nContent-Length: %d\n" % 
> len(content)
>             print content, '<br /><br />'

You really don't understand how the web works, do you?

In order to include an image in a page your browser must make TWO 
requests. The first is for an HTML page that will reference the image in 
this way:

     <img src="http://your URL here">

Seeing this img tag causes the browser to make a SECOND request, which 
the script I corrected above should respond to with an image.

The bytestream is critical in the image response. Even one misplaced 
byte will mess things up terribly.

> ...
>   except:
>     if lang == 'es':
>       print 'Lo siento. Todavía no tenemos propiedades para enseñarse en 
> la categoría de ', category, '.\n<br /><br />'
> 
>  
> 
>     You are NOT generating HTML, you are generating a JPEG image.
> 
> 
> I am producing both, and this is causing me confusion.

Read an explanation of HTML and images if my brief treatise above was 
insufficient.

> BTW, when we are finally done with this, I will write a nice how-to 
> (since there is not one in python, while php has some nice ones) on how 
> to do this, and give you and Gabrielle all your due credit. I will post 
> it to this list, because that is sure to rank highly in google right away.
> Victor
> 
That's great, though hardly the point of the exercise. I think Google 
already know about Gabriel (*not* Gabrielle) and me already ...

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