String Literal to Blob

Steve Holden steve at holdenweb.com
Wed Apr 9 13:49:30 EDT 2008


Victor Subervi wrote:
> On Wed, Apr 9, 2008 at 12:51 PM, Steve Holden <steve at holdenweb.com 
> <mailto:steve at holdenweb.com>> wrote:
> 
>     Victor Subervi wrote:
> 
>         On Wed, Apr 9, 2008 at 11:10 AM, Steve Holden
>         <steve at holdenweb.com <mailto:steve at holdenweb.com>
>         <mailto:steve at holdenweb.com <mailto:steve at holdenweb.com>>> wrote:
> 
>     I'm having a problem believing this, but I don't think you are
>     lying. Are you *sure* you have stored the correct omages in your
>     database?
> 
>  
> Well, the Plesk PHP/MySQL interface indicates that a blob has been 
> successfully stored. Furthermore, the output I get has strings like 
> either 'Adobe Photoshop' or 'GIMP', depending on the editor I used. And 
> the output is like I have seen before from incorrectly rendered images. 
> And images are what I loaded into those fields through my form. So I 
> believe they are indeed images.
>  
> 
>     The fact remains that cursor.fetchall() will return a list
>     containing one tuple containing (what you believe is) your image, so
>     there is NO way your code above can do what you want.
> 
>  
> Right. I used your suggestion of cursor.fetchall()[0][0] and the result 
> was *still* the image of the url. (I also used the other suggestion.)
>  
> 
> 
> 
>     I can therefore only assume that this is a CGI script and that your
>     web server does something *extremely* funky when it gets a CGI
>     output it isn't expecting. But this doesn't make a lot of sense. 
> 
>  
> Okay. How trouble-shoot this? Pass it on to the techies where I host? 
> Generally they are less than receptive, but maybe if I show them this 
> thread I can get their attention.
>  
Nope, no need to start berating unresponsive techies yet. Instead, try 
writing a file and see whether it is a legitimate JPEG or not.

I imagine the following code should do so, given your earlier writings:

#! /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()[0][0]
f = open("somefile.jpg", "w")
f.write(content)
f.close()

Then see if you can deal with "somefile.jpg" like any other JPEG. If you 
can't then your blobs are somehow being mangled. If you can, we'll take 
it from there.

regards
  Steve

> 
> 
> 
>     Stupidity and ignorance are entirely different things, and you
>     (current) ignorance in no way implies stupidity. We all have to learn.
> 
>  
> True. I have never found programming easy. But I have been very 
> persistent. It still is not easy for me.
>  
> 
>     However, if you bring up one of the pages from one of your many web
>     sites containing an image, and get your browser to display the HTML
>     of that page you will surely find that the image does not appear
>     direectly in the HTML, but instead appears as a tag in the HTML.
>     Something like:
> 
>        <img src="http://server/path/image.jpg">
> 
>     though the src attribute doesn't really need to be that complex. 
> 
>  
> Of course.
>  
> 
>      
> 
>         In my stupidity, I have assumed you meant this:
> 
>                    content = col_fields[0][14].tostring()
>                    print '<img src="', content, '"><br /><br />'
> 
>     Well, here I have no idea what the content of your database might
>     be, but if the fifteenth column you retrieve is the web server path
>     to the graphic, that should be right except for the spaces around
>     it, which might give trouble. You might consider instead
> 
> 
>                content = col_fields[0][14].tostring()
>                print '<img src="%s"><br /><br />' % content
> 
>  
> Great suggestion! Those spaces always mess me up. Unfortunately, it 
> *still* did not render properly :(
>  
> 
>          
> 
>     Forget HTML for now. If you direct your browser to the URL on which
>     your server is serving the graphic then it should be displayed in
>     the browser window. Until that happy condition pertains, we are
>     stabbing around in the dark. 
> 
>  
> Again...time to alert the teckies where I host?
>  
> 
>      
>     It's not that I mind, but I do feel that this knowledge is already
>     available, though clearly I might be wrong ... 
> 
>  
> Well, I may have missed it in all my googling, but I thought I was 
> pretty thorough. At any rate, it certainly cannot hurt to document it 
> again (if it is indeed 'again')
> TIA,
> Victor
> 


-- 
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