Re: String Literal to Blob

2008-04-16 Thread Victor Subervi
The _keywords_ are _essential_. It is currently published at the end of a
long and exhaustive thread. This is not good. It should be republished
correctly, and with the kw people will use to search. For example, I would
never have thought to search for a photo album.
Victor

On Tue, Apr 15, 2008 at 10:55 AM, J. Cliff Dyer <[EMAIL PROTECTED]> wrote:

> It is published.  On comp.lang.python.  Google groups has it, so google
> (search) will find it.
>
> Cheers,
> Cliff
>
>
> On Tue, 2008-04-15 at 17:04 +0200, Victor Subervi wrote:
> > Gabriel;
> >
> > That's really nice code you wrote. I will rewrite my app accordingly,
> > after I catch a breather! Say, would you please publish this
> > somewhere? Why should I write a howto on this and credit you when all
> > I would be doing is republishing (plagerizing) what you published?
> > Please insert these keywords: mysql, image, python, mysqldb and maybe
> > picture and photo (you already have photo). Call it something like
> > "MySQL/Python Tutorial for Posting and Retrieving Images / Photo
> > Album". I ask you to do this because I scoured google looking for just
> > what you've provided and it simply isn't out there. At all. There are
> > nice howto's in php. Please post this for those interested in python,
> > somewhere like the cookbook.
> >
> > Thanks,
> >
> > Victor
> >
> >
> >
> > On Tue, Apr 15, 2008 at 3:23 AM, Gabriel Genellina
> > <[EMAIL PROTECTED]> wrote:
> > En Mon, 14 Apr 2008 11:03:54 -0300, Steve Holden
> > <[EMAIL PROTECTED]>
> > escribió:
> > > Victor Subervi wrote:
> > >> Thanks to all, especially Gabriel. [...]
> > >> Steve, thank you for all your help, but do overcome your
> > temper :))
> > >
> >
> > > I'm glad the penny finally dropped. You may have been
> > treated to a
> > > modest display of exasperation, but please be assured you
> > have not yet
> > > seen anything remotely like temper from me :-)
> >
> >
> > And I'm glad to see that you finally "got it", too!
> >
> > --
> > Gabriel Genellina
> >
> > --
> >
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
> --
> Oook,
> J. Cliff Dyer
> Carolina Digital Library and Archives
> UNC Chapel Hill
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-15 Thread J. Cliff Dyer
It is published.  On comp.lang.python.  Google groups has it, so google
(search) will find it.

Cheers,
Cliff


On Tue, 2008-04-15 at 17:04 +0200, Victor Subervi wrote:
> Gabriel;
> 
> That's really nice code you wrote. I will rewrite my app accordingly,
> after I catch a breather! Say, would you please publish this
> somewhere? Why should I write a howto on this and credit you when all
> I would be doing is republishing (plagerizing) what you published?
> Please insert these keywords: mysql, image, python, mysqldb and maybe
> picture and photo (you already have photo). Call it something like
> "MySQL/Python Tutorial for Posting and Retrieving Images / Photo
> Album". I ask you to do this because I scoured google looking for just
> what you've provided and it simply isn't out there. At all. There are
> nice howto's in php. Please post this for those interested in python,
> somewhere like the cookbook.
> 
> Thanks,
> 
> Victor
> 
> 
> 
> On Tue, Apr 15, 2008 at 3:23 AM, Gabriel Genellina
> <[EMAIL PROTECTED]> wrote:
> En Mon, 14 Apr 2008 11:03:54 -0300, Steve Holden
> <[EMAIL PROTECTED]>
> escribió:
> > Victor Subervi wrote:
> >> Thanks to all, especially Gabriel. [...]
> >> Steve, thank you for all your help, but do overcome your
> temper :))
> >
> 
> > I'm glad the penny finally dropped. You may have been
> treated to a
> > modest display of exasperation, but please be assured you
> have not yet
> > seen anything remotely like temper from me :-)
> 
> 
> And I'm glad to see that you finally "got it", too!
> 
> --
> Gabriel Genellina
> 
> --
> 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 
-- 
Oook,
J. Cliff Dyer
Carolina Digital Library and Archives
UNC Chapel Hill

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-15 Thread Victor Subervi
Gabriel;

That's really nice code you wrote. I will rewrite my app accordingly, after
I catch a breather! Say, would you please publish this somewhere? Why should
I write a howto on this and credit you when all I would be doing is
republishing (plagerizing) what you published? Please insert these keywords:
mysql, image, python, mysqldb and maybe picture and photo (you already have
photo). Call it something like "MySQL/Python Tutorial for Posting and
Retrieving Images / Photo Album". I ask you to do this because I scoured
google looking for just what you've provided and it simply isn't out there.
At all. There are nice howto's in php. Please post this for those interested
in python, somewhere like the cookbook.

Thanks,

Victor


On Tue, Apr 15, 2008 at 3:23 AM, Gabriel Genellina <[EMAIL PROTECTED]>
wrote:

> En Mon, 14 Apr 2008 11:03:54 -0300, Steve Holden <[EMAIL PROTECTED]>
> escribió:
> > Victor Subervi wrote:
> >> Thanks to all, especially Gabriel. [...]
> >> Steve, thank you for all your help, but do overcome your temper :))
> >
> > I'm glad the penny finally dropped. You may have been treated to a
> > modest display of exasperation, but please be assured you have not yet
> > seen anything remotely like temper from me :-)
>
> And I'm glad to see that you finally "got it", too!
>
> --
> Gabriel Genellina
>
> --
>  http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-14 Thread Gabriel Genellina
En Mon, 14 Apr 2008 11:03:54 -0300, Steve Holden <[EMAIL PROTECTED]>  
escribió:
> Victor Subervi wrote:
>> Thanks to all, especially Gabriel. [...]
>> Steve, thank you for all your help, but do overcome your temper :))
>
> I'm glad the penny finally dropped. You may have been treated to a
> modest display of exasperation, but please be assured you have not yet
> seen anything remotely like temper from me :-)

And I'm glad to see that you finally "got it", too!

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-14 Thread Steve Holden
Victor Subervi wrote:
> Thanks to all, especially Gabriel. The base64 is a good idea, but you 
> state a definite problem. I will look at your code at home 
> (offline)...thank you very much! It looks like the kicker is this line here:
>  
> " % (picid, cgi.escape(title))
>  
> Now, why didn´t you share that before? I can see how calling a 
> separate script like that would work! Again, you should have shared that 
> before. How was I to think of that clever trick from the bare 
> information you gave me earlier??
>  
> Steve, thank you for all your help, but do overcome your temper :))

Victor:

I'm glad the penny finally dropped. You may have been treated to a 
modest display of exasperation, but please be assured you have not yet 
seen anything remotely like temper from me :-)

The thing I found difficult to understand was, given your assertion on 
April 9 "But I am persistent. And I have built dozens of Web site with 
images" that you hadn't already addressed these issues. I was clearly 
assuming too much knowledge on your part.

Hooray! You got it! Your persistence finally paid off, well done!

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-14 Thread Victor Subervi
Thanks to all, especially Gabriel. The base64 is a good idea, but you state
a definite problem. I will look at your code at home (offline)...thank you
very much! It looks like the kicker is this line here:

" % (picid, cgi.escape(title))

Now, why didn´t you share that before? I can see how calling a separate
script like that would work! Again, you should have shared that before. How
was I to think of that clever trick from the bare information you gave me
earlier??

Steve, thank you for all your help, but do overcome your temper :))
Victor

On Sun, Apr 13, 2008 at 7:05 AM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Jason Scheirer wrote:
> [...]
> >
> > There _is_ a way to embed image data in HTML that is supported by
> > every major browser. It is ugly. Using the RFC 2397 (http://
> > www.ietf.org/rfc/rfc2397) spec for data URLs you could go
> >
> > '' % base64.b64encode(image_data)
> >
> > Obviously you need to import the base64 module somewhere in your code
> > and base64-encoded data is about a third larger than it would be
> > otherwise, so embedding anything particularly large is going to be a
> > huge pain and affect page load times pretty badly.
>
> This is hardly likely to help someone who hasn't yet grasped the concept
> of referencing graphics and prefers to write database output to disk to
> serve it statically. But who knows, maybe it will ...
>
> regards
>  Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
>  --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-13 Thread Steve Holden
Jason Scheirer wrote:
[...]
> 
> There _is_ a way to embed image data in HTML that is supported by
> every major browser. It is ugly. Using the RFC 2397 (http://
> www.ietf.org/rfc/rfc2397) spec for data URLs you could go
> 
> '' % base64.b64encode(image_data)
> 
> Obviously you need to import the base64 module somewhere in your code
> and base64-encoded data is about a third larger than it would be
> otherwise, so embedding anything particularly large is going to be a
> huge pain and affect page load times pretty badly.

This is hardly likely to help someone who hasn't yet grasped the concept 
of referencing graphics and prefers to write database output to disk to 
serve it statically. But who knows, maybe it will ...

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-12 Thread Gabriel Genellina
En Sat, 12 Apr 2008 16:11:20 -0300, Victor Subervi  
<[EMAIL PROTECTED]> escribió:
On Fri, Apr 11, 2008 at 2:05 PM, Steve Holden <[EMAIL PROTECTED]>  
wrote:



Then browse to the URL this program serves and you will see the image
(assuming you are still sending the image/jpeg content type).


Well, as I mentioned before, I am sending text/html because the page,  
like

almost all web pages, has a whole lot more content than just images. Or,
perhaps you are suggesting I build my pages in frames, and have a frame  
for

every image. Unsightly!


(Oh my...!) Thanks to Helloween (the metal band) I was in the right mood  
to write a small example. Simple, bare, raw code: no fancy HTML, no  
templates, no web frameworks, using the embedded sqlite engine. It works  
with Python 2.5 out of the box. Each functional piece is in its own file,  
to emphasize the one-document-per-request rule. It is a photo album, one  
can upload pictures with title and description, and later see them one at  
a time.


How to run: Create a temporary folder. Create a cgi-bin subdirectory, and  
copy the four scripts there. In the parent directory, execute: python -m  
CGIHTTPServer
Using your browser, go to http://localhost:8000/cgi-bin/album.py?init=1  
(the init parameter is to create the database; you can omit it once it's  
done).


Note that there are TWO scripts involved when you want to display a  
picture: showpic.py returns the HTML page, containing tags like this:

" % (picid, cgi.escape(title))
(note the src attribute) and getpic.py returns the picture itself.

Note also that this is just a demo; for a real web application I would  
choose one of the many existing frameworks; or WSGI, or mod_python,  
finally cgi. And my scripts have absolutely no check for errors at all,  
and the HTML is not even valid.


I hope you finally get the idea of how to serve dynamic content...

--
Gabriel Genellina

upload.py
Description: Binary data


album.py
Description: Binary data


getpic.py
Description: Binary data


showpic.py
Description: Binary data
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-12 Thread Gabriel Genellina
En Sat, 12 Apr 2008 22:14:31 -0300, Jason Scheirer  
<[EMAIL PROTECTED]> escribió:
> On Apr 12, 2:44 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
>> Victor Subervi wrote:

>> > Well, as I mentioned before, I am sending text/html because the page,
>> > like almost all web pages, has a whole lot more content than just
>> > images. Or, perhaps you are suggesting I build my pages in frames, and
>> > have a frame for every image. Unsightly!
>>
>> Dear Victor:
>>
>> If you cannot understand, after being told several times by different
>> people, that pages with images in them are achieved by multiple HTTP
>> requests, then there is little I can do to help you.
>> [...]
>> Please, do yourself a big favor and persist with this until you
>> understand what you are doing wrong and how to serve dynamic images. It
>> appears that the learning may be painful, but I guarantee it will be
>> worthwhile.
>
> There _is_ a way to embed image data in HTML that is supported by
> every major browser. It is ugly. Using the RFC 2397 (http://
> www.ietf.org/rfc/rfc2397) spec for data URLs you could go
>
> '' % base64.b64encode(image_data)

Another alternative would be to generate a multipart/related document, but  
I think the OP will gain a lot more understanding the simple cases than  
using those esoteric features.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-12 Thread Jason Scheirer
On Apr 12, 2:44 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
> Victor Subervi wrote:
> > in line...
>
> > On Fri, Apr 11, 2008 at 2:05 PM, Steve Holden <[EMAIL PROTECTED]
> > > wrote:
>
> > Victor Subervi wrote:
> >  > I have worked on this many hours a day for two weeks. If there is an
> >  > easier way to do it, just take a minute or two and point it out. Have
> >  > you heard of the Law of Diminishing Returns? I have passed it
> > long ago.
> >  > I no longer want to waste time trying to guess at what you are
> > trying to
> >  > tell me.
> >  > Victor
>
> >  > On Fri, Apr 11, 2008 at 8:55 AM, Steve Holden
> > <[EMAIL PROTECTED] 
> >  > >> wrote:
> > Where you have
>
> > content = col_fields[0][14].tostring()
> > pic = "tmp" + str(i) + ".jpg"
> > img = open(pic, "w")
> > img.write(content)
> > print '' % pic
> > img.close()
>
> > instead write
>
> > print content
>
> > Like this, I presume?
>
> Yes. You might need to use content.tostring() - I am not familiar with
> MySQL blobs.
>
> > img = open(pic, "w")
> > img.write(content)
> > print ' > value="%s">' % pic
> > print content
> > #print '\n' % pic
> > Does not work _at_all LOL. You will recall, also, that you once gave me
> > a line similar to the one commented out (but without writing then
> > opening the file). THAT did not work, either. So now do you see why I am
> > frustrated??
>
> > Then browse to the URL this program serves and you will see the image
> > (assuming you are still sending the image/jpeg content type).
>
> > Well, as I mentioned before, I am sending text/html because the page,
> > like almost all web pages, has a whole lot more content than just
> > images. Or, perhaps you are suggesting I build my pages in frames, and
> > have a frame for every image. Unsightly!
>
> Dear Victor:
>
> If you cannot understand, after being told several times by different
> people, that pages with images in them are achieved by multiple HTTP
> requests, then there is little I can do to help you.
>
>
>
> > Once you
> > can see the image, THEN you can write a page that refers to it. Until
> > you start serving the image (NOT pseudo-html with image data embedded in
> >  it) nothing else will work.
>
> > My solution works just fine, thank you. It is inelegant. But it now
> > appears to me, and I dare say rather clearly, that this inelegance is
> > the fault of python itself. Perhaps this should be brought to Guido´s
> > attention.
> > Victor
>
> You can say it as clearly as you like, but if you say it too loudly you
> will make a laughing stock of yourself.
>
> You surely don't think that a language that supports Zope, TurboGears,
> Pylons and Django (to name but the first four that come to mind) is
> unsuitable for web programming?
>
> Please, do yourself a big favor and persist with this until you
> understand what you are doing wrong and how to serve dynamic images. It
> appears that the learning may be painful, but I guarantee it will be
> worthwhile.
>
> regards
>   Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/

There _is_ a way to embed image data in HTML that is supported by
every major browser. It is ugly. Using the RFC 2397 (http://
www.ietf.org/rfc/rfc2397) spec for data URLs you could go

'' % base64.b64encode(image_data)

Obviously you need to import the base64 module somewhere in your code
and base64-encoded data is about a third larger than it would be
otherwise, so embedding anything particularly large is going to be a
huge pain and affect page load times pretty badly.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-12 Thread Steve Holden
One last thing: I am sorry that despite my efforts I was unable to teach 
you what you need to learn. I can only hope someone else manages to get 
the point across.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-12 Thread Steve Holden
Victor Subervi wrote:
> in line...
> 
> On Fri, Apr 11, 2008 at 2:05 PM, Steve Holden <[EMAIL PROTECTED] 
> > wrote:
> 
> Victor Subervi wrote:
>  > I have worked on this many hours a day for two weeks. If there is an
>  > easier way to do it, just take a minute or two and point it out. Have
>  > you heard of the Law of Diminishing Returns? I have passed it
> long ago.
>  > I no longer want to waste time trying to guess at what you are
> trying to
>  > tell me.
>  > Victor
>  >
>  > On Fri, Apr 11, 2008 at 8:55 AM, Steve Holden
> <[EMAIL PROTECTED] 
>  > >> wrote:
> Where you have
> 
> content = col_fields[0][14].tostring()
> pic = "tmp" + str(i) + ".jpg"
> img = open(pic, "w")
> img.write(content)
> print '' % pic
> img.close()
> 
> instead write
> 
> print content
> 
>  
> Like this, I presume?
>  
Yes. You might need to use content.tostring() - I am not familiar with 
MySQL blobs.

> img = open(pic, "w")
> img.write(content)
> print ' value="%s">' % pic
> print content
> #print '\n' % pic
> Does not work _at_all LOL. You will recall, also, that you once gave me 
> a line similar to the one commented out (but without writing then 
> opening the file). THAT did not work, either. So now do you see why I am 
> frustrated??
> 
> 
> 
> Then browse to the URL this program serves and you will see the image
> (assuming you are still sending the image/jpeg content type). 
> 
>  
> Well, as I mentioned before, I am sending text/html because the page, 
> like almost all web pages, has a whole lot more content than just 
> images. Or, perhaps you are suggesting I build my pages in frames, and 
> have a frame for every image. Unsightly!
>  
Dear Victor:

If you cannot understand, after being told several times by different 
people, that pages with images in them are achieved by multiple HTTP 
requests, then there is little I can do to help you.

> 
> Once you
> can see the image, THEN you can write a page that refers to it. Until
> you start serving the image (NOT pseudo-html with image data embedded in
>  it) nothing else will work.
> 
>  
> My solution works just fine, thank you. It is inelegant. But it now 
> appears to me, and I dare say rather clearly, that this inelegance is 
> the fault of python itself. Perhaps this should be brought to Guido´s 
> attention.
> Victor

You can say it as clearly as you like, but if you say it too loudly you 
will make a laughing stock of yourself.

You surely don't think that a language that supports Zope, TurboGears, 
Pylons and Django (to name but the first four that come to mind) is 
unsuitable for web programming?

Please, do yourself a big favor and persist with this until you 
understand what you are doing wrong and how to serve dynamic images. It 
appears that the learning may be painful, but I guarantee it will be 
worthwhile.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-12 Thread Victor Subervi
in line...

On Fri, Apr 11, 2008 at 2:05 PM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Victor Subervi wrote:
> > I have worked on this many hours a day for two weeks. If there is an
> > easier way to do it, just take a minute or two and point it out. Have
> > you heard of the Law of Diminishing Returns? I have passed it long ago.
> > I no longer want to waste time trying to guess at what you are trying to
> > tell me.
> > Victor
> >
> > On Fri, Apr 11, 2008 at 8:55 AM, Steve Holden <[EMAIL PROTECTED]
> > > wrote:
> Where you have
>
> content = col_fields[0][14].tostring()
> pic = "tmp" + str(i) + ".jpg"
> img = open(pic, "w")
> img.write(content)
> print '' % pic
> img.close()
>
> instead write
>
> print content


Like this, I presume?

img = open(pic, "w")
img.write(content)
print '' % pic
print content
#print '\n' % pic
Does not work _at_all LOL. You will recall, also, that you once gave me a
line similar to the one commented out (but without writing then opening the
file). THAT did not work, either. So now do you see why I am frustrated??

>
>
> Then browse to the URL this program serves and you will see the image
> (assuming you are still sending the image/jpeg content type).


Well, as I mentioned before, I am sending text/html because the page, like
almost all web pages, has a whole lot more content than just images. Or,
perhaps you are suggesting I build my pages in frames, and have a frame for
every image. Unsightly!


> Once you
> can see the image, THEN you can write a page that refers to it. Until
> you start serving the image (NOT pseudo-html with image data embedded in
>  it) nothing else will work.


My solution works just fine, thank you. It is inelegant. But it now appears
to me, and I dare say rather clearly, that this inelegance is the fault of
python itself. Perhaps this should be brought to Guido´s attention.
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-11 Thread Steve Holden
Victor Subervi wrote:
> I have worked on this many hours a day for two weeks. If there is an 
> easier way to do it, just take a minute or two and point it out. Have 
> you heard of the Law of Diminishing Returns? I have passed it long ago. 
> I no longer want to waste time trying to guess at what you are trying to 
> tell me.
> Victor
> 
> On Fri, Apr 11, 2008 at 8:55 AM, Steve Holden <[EMAIL PROTECTED] 
> > wrote:
> 
> Victor Subervi wrote:
>  > Nope. Do not see it. My ugly stupid way works. I guess I will just
>  > proceed with that and write my howto accordingly.
>  > Victor
>  >
> OK, but be prepared for some pretty scathing feedback. You will make it
> clear you do not understand the web. I'd suggest a little more reading
> first.
> 
You aren't the only one whose patience and endurance is being tried by 
this thread.

Take a look, for the want of anything better, at

   http://holdenweb.com/

View the source of that page, and the first image you will find inside 
it reads

 

Then direct your browser to

 http://holdenweb.com/images/homepage/hd_explore.png

and you will see just the image. After the change I detail below, you 
will have turned your script into the dynamic equivalent of the static 
image I directed you to above.
Where you have

 content = col_fields[0][14].tostring()
 pic = "tmp" + str(i) + ".jpg"
 img = open(pic, "w")
 img.write(content)
 print '' % pic
 img.close()

instead write

 print content

Then browse to the URL this program serves and you will see the image 
(assuming you are still sending the image/jpeg content type). Once you 
can see the image, THEN you can write a page that refers to it. Until 
you start serving the image (NOT pseudo-html with image data embedded in 
  it) nothing else will work.

Then you can start writing  tags that include your (dyanmic) 
URLs and have your script serve the images retrieved from the database.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-11 Thread Victor Subervi
I have worked on this many hours a day for two weeks. If there is an easier
way to do it, just take a minute or two and point it out. Have you heard of
the Law of Diminishing Returns? I have passed it long ago. I no longer want
to waste time trying to guess at what you are trying to tell me.
Victor

On Fri, Apr 11, 2008 at 8:55 AM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Victor Subervi wrote:
> > Nope. Do not see it. My ugly stupid way works. I guess I will just
> > proceed with that and write my howto accordingly.
> > Victor
> >
> OK, but be prepared for some pretty scathing feedback. You will make it
> clear you do not understand the web. I'd suggest a little more reading
> first.
>
> regards
>  Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
> --
>  http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-11 Thread Steve Holden
Victor Subervi wrote:
> Nope. Do not see it. My ugly stupid way works. I guess I will just 
> proceed with that and write my howto accordingly.
> Victor
> 
OK, but be prepared for some pretty scathing feedback. You will make it 
clear you do not understand the web. I'd suggest a little more reading 
first.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-11 Thread Victor Subervi
Nope. Do not see it. My ugly stupid way works. I guess I will just proceed
with that and write my howto accordingly.
Victor

On Thu, Apr 10, 2008 at 9:01 PM, Gabriel Genellina <[EMAIL PROTECTED]>
wrote:

> En Thu, 10 Apr 2008 14:04:43 -0300, Victor Subervi
> <[EMAIL PROTECTED]> escribió:
>
> > Well, what I did was this:
> >
> > content = col_fields[0][14].tostring()
> > pic = "tmp" + str(i) + ".jpg"
> > img = open(pic, "w")
> > img.write(content)
> > print '' % pic
> > img.close()
> > where I am incrementing i. Ugly. Stupid. But if it is the only way to do
> > it
> > in python, and I do not want to invest the time doing it in php, which I
> > think would be prettier in this instance, then I guess it will do. Your
> > thoughts appreciated.
>
> You REALLY should read some material on how HTTP works. I'll try to sketch
> a few important points. First, suppose you have an HTML page (album.html)
> with two images in it:
>
> 
> This is me: 
> and this is my cat 
> 
>
> Suppose the URL for that page is http://some.server.com/gabriel/album.html
> and you type that in your favorite browser. This is what happens:
> 1) The sees the initial "http:" and says "I'll use HTTP". Then sees
> "some.server.com" and opens a connection to that server on port 80. Then
> sees "/gabriel.album.html" and builds an HTTP GET request for it.
> 2) The server receives the GET request, looks for the "album.html"
> document, determines the right Content-Type, and returns it specifying
> "Content-Type: text/html"
> 3) The browser receives the HTML text and tries to display it. When it
> encounters the first  tag it looks at the src attribute; it doesn't
> know that image; so a *NEW* HTTP request is required. This time it says
> "GET /images/myself.jpg"
> 4) The server receives the GET request, looks for a file with that name,
> determines that it's a jpeg image, and returns its contents along with a
> "Content-Type: image/jpeg".
> 5) The browser receives the image and is able to display it.
> 6) The same thing happens with the second  tag, there is a third HTTP
> GET request for it.
>
> Note that:
> - The images themselves *aren't* in the HTML page, they are somewhere
> else. HTML is text and contains ONLY the URI for the image.
> - THREE DIFFERENT requests are done to show that page. Each one returns A
> SINGLE OBJECT of A SINGLE TYPE.
>
> The above was using static HTML with static images. If you use CGI to
> generate dynamic content, it's the same thing. From the browser point of
> view, there is no difference: it still will generate three different
> requests for the three pieces (one html document with two images).
> Your CGI script (or scripts) will receive three different requests then:
> when requested for HTML, return HTML; when requested for an image, return
> an image. They are DIFFERENT things, DIFFERENT requests, happening at
> DIFFERENT times, so don't mix them.
>
> I think that combining Steve's responses and mine you now have enough
> information to be able to solve your problems. Perhaps if you re-read the
> whole thread from start you'll have now a better understanding of what's
> happening.
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-10 Thread Gabriel Genellina
En Thu, 10 Apr 2008 14:04:43 -0300, Victor Subervi  
<[EMAIL PROTECTED]> escribió:

> Well, what I did was this:
>
> content = col_fields[0][14].tostring()
> pic = "tmp" + str(i) + ".jpg"
> img = open(pic, "w")
> img.write(content)
> print '' % pic
> img.close()
> where I am incrementing i. Ugly. Stupid. But if it is the only way to do  
> it
> in python, and I do not want to invest the time doing it in php, which I
> think would be prettier in this instance, then I guess it will do. Your
> thoughts appreciated.

You REALLY should read some material on how HTTP works. I'll try to sketch  
a few important points. First, suppose you have an HTML page (album.html)  
with two images in it:


This is me: 
and this is my cat 


Suppose the URL for that page is http://some.server.com/gabriel/album.html  
and you type that in your favorite browser. This is what happens:
1) The sees the initial "http:" and says "I'll use HTTP". Then sees  
"some.server.com" and opens a connection to that server on port 80. Then  
sees "/gabriel.album.html" and builds an HTTP GET request for it.
2) The server receives the GET request, looks for the "album.html"  
document, determines the right Content-Type, and returns it specifying  
"Content-Type: text/html"
3) The browser receives the HTML text and tries to display it. When it  
encounters the first  tag it looks at the src attribute; it doesn't  
know that image; so a *NEW* HTTP request is required. This time it says  
"GET /images/myself.jpg"
4) The server receives the GET request, looks for a file with that name,  
determines that it's a jpeg image, and returns its contents along with a  
"Content-Type: image/jpeg".
5) The browser receives the image and is able to display it.
6) The same thing happens with the second  tag, there is a third HTTP  
GET request for it.

Note that:
- The images themselves *aren't* in the HTML page, they are somewhere  
else. HTML is text and contains ONLY the URI for the image.
- THREE DIFFERENT requests are done to show that page. Each one returns A  
SINGLE OBJECT of A SINGLE TYPE.

The above was using static HTML with static images. If you use CGI to  
generate dynamic content, it's the same thing. From the browser point of  
view, there is no difference: it still will generate three different  
requests for the three pieces (one html document with two images).
Your CGI script (or scripts) will receive three different requests then:  
when requested for HTML, return HTML; when requested for an image, return  
an image. They are DIFFERENT things, DIFFERENT requests, happening at  
DIFFERENT times, so don't mix them.

I think that combining Steve's responses and mine you now have enough  
information to be able to solve your problems. Perhaps if you re-read the  
whole thread from start you'll have now a better understanding of what's  
happening.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-10 Thread Steve Holden
Victor Subervi wrote:
> Well, what I did was this:
>  
> content = col_fields[0][14].tostring()
> pic = "tmp" + str(i) + ".jpg"
> img = open(pic, "w")
> img.write(content)
> print '' % pic
> img.close()
> where I am incrementing i. Ugly. Stupid. But if it is the only way to do 
> it in python, and I do not want to invest the time doing it in php, 
> which I think would be prettier in this instance, then I guess it will 
> do. Your thoughts appreciated.
> Victor
> 
It is not the only way to do it on Python, it's not even an especially 
good way. But I suggest you either read what I have already written more 
carefully, or study HTTP and HTML until you understand what you are 
doing unnecessarily wrong. There is absolutely no need to save the 
images to the file store (otherwise there would be no point in having 
them in the database in the first place). You simply need to understand 
how to serve them dynamically.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-10 Thread Victor Subervi
Well, what I did was this:

content = col_fields[0][14].tostring()
pic = "tmp" + str(i) + ".jpg"
img = open(pic, "w")
img.write(content)
print '' % pic
img.close()
where I am incrementing i. Ugly. Stupid. But if it is the only way to do it
in python, and I do not want to invest the time doing it in php, which I
think would be prettier in this instance, then I guess it will do. Your
thoughts appreciated.
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-10 Thread Steve Holden
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 '
> 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 '' % 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:

   

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/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-10 Thread Victor Subervi
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 '' % 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-09 Thread Victor Subervi
On Wed, Apr 9, 2008 at 1:49 PM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Victor Subervi wrote:
> > On Wed, Apr 9, 2008 at 12:51 PM, Steve Holden <[EMAIL PROTECTED]
> > > wrote:
> 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.


Yes, I can. Shows up in browser when surfed to, and it was pulled from the
database. No mangling. I am signing off, back tomorrow morning.
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-09 Thread Steve Holden
Victor Subervi wrote:
> On Wed, Apr 9, 2008 at 12:51 PM, Steve Holden <[EMAIL PROTECTED] 
> > wrote:
> 
> Victor Subervi wrote:
> 
> On Wed, Apr 9, 2008 at 11:10 AM, Steve Holden
> <[EMAIL PROTECTED] 
> >> 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:
> 
>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 ''
> 
> 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 '' % 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/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-09 Thread Victor Subervi
On Wed, Apr 9, 2008 at 12:51 PM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Victor Subervi wrote:
>
> > On Wed, Apr 9, 2008 at 11:10 AM, Steve Holden <[EMAIL PROTECTED] > [EMAIL PROTECTED]>> 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.


>
>
> 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:
>
>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 ''
> >
> > 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 '' % 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-09 Thread Steve Holden
Victor Subervi wrote:
> On Wed, Apr 9, 2008 at 11:10 AM, Steve Holden <[EMAIL PROTECTED] 
> > wrote:
> 
> Victor Subervi wrote:
> 
> On Wed, Apr 9, 2008 at 10:24 AM, Steve Holden
> <[EMAIL PROTECTED] 
> >> wrote:
> 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]
> 
> 
> I tried both of these, and they, too, gave me the same identical image 
> of the url, not the image I am after.
> 
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?

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.

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.

> 
> 
> You really don't understand how the web works, do you?
> 
> 
> I am just really, really, stupid, Steve. Please forgive me for being so 
> stupid. But I am persistent. And I have built dozens of Web site with 
> images.
>  
Stupidity and ignorance are entirely different things, and you (current) 
ignorance in no way implies stupidity. We all have to learn.

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:

 http://server/path/image.jpg";>

though the src attribute doesn't really need to be that complex.
> 
> 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:
> 
>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.
> 
> 
> In my stupidity, I have assumed you meant this:
> 
> content = col_fields[0][14].tostring()
> print ''
> 
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 '' % content

to omit them.

> Obviously I am wrong. Could you please give me a little more insight?
> 
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.

> 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 ...
> 
> 
> Well, I just thought it might be a good way to get the information out, 
> since it is not out there yet. And I believe it is only proper to give 
> credit where credit is due. I would not be doing this to praise you. 
> Rather, I would be doing this because it is needed, and I would feel 
> wrong for not crediting those who deserve credit. Please let me know, 
> however, if you feel otherwise.

It's not that I mind, but I do feel that this knowledge is already 
available, though clearly I might be wrong ...

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-09 Thread Victor Subervi
On Wed, Apr 9, 2008 at 11:10 AM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Victor Subervi wrote:
>
> > On Wed, Apr 9, 2008 at 10:24 AM, Steve Holden <[EMAIL PROTECTED] > [EMAIL PROTECTED]>> wrote:
> > 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]


I tried both of these, and they, too, gave me the same identical image of
the url, not the image I am after.

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


I am just really, really, stupid, Steve. Please forgive me for being so
stupid. But I am persistent. And I have built dozens of Web site with
images.


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


In my stupidity, I have assumed you meant this:

content = col_fields[0][14].tostring()
print ''

Obviously I am wrong. Could you please give me a little more insight?

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


Well, I just thought it might be a good way to get the information out,
since it is not out there yet. And I believe it is only proper to give
credit where credit is due. I would not be doing this to praise you. Rather,
I would be doing this because it is needed, and I would feel wrong for not
crediting those who deserve credit. Please let me know, however, if you feel
otherwise.
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-09 Thread Steve Holden
Victor Subervi wrote:
> On Wed, Apr 9, 2008 at 10:24 AM, Steve Holden <[EMAIL PROTECTED] 
> > wrote:
> 
> Victor Subervi wrote:
>  > On Wed, Apr 9, 2008 at 1:14 AM, Gabriel Genellina
>  > <[EMAIL PROTECTED] 
> >> 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 '\n'
>   cursor.execute('select * from products where id = ' + str(d) + 
> ';')
>   col_fields = cursor.fetchall()
>   if lang == 'es':
> print 'ID: ', col_fields[0][0], ''
> print 'Nombre: ', col_fields[0][2], ''
> print 'Título: ', col_fields[0][6], ''
> print 'Descripción: ', col_fields[0][9], ''
> print 'Precio: ', col_fields[0][11], ''
> print 'Recámaras: ', col_fields[0][12], ''
> print 'Baños: ', col_fields[0][13], ''
> content = col_fields[0][14].tostring()
> print "Content-Type: image/jpeg\nContent-Length: %d\n" % 
> len(content)
> print content, ''

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:

 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'
> 
>  
> 
> 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/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-09 Thread Victor Subervi
On Wed, Apr 9, 2008 at 10:24 AM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Victor Subervi wrote:
> > On Wed, Apr 9, 2008 at 1:14 AM, Gabriel Genellina
> > <[EMAIL PROTECTED] > 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()
print "Content-Type: image/jpeg\nContent-Length: %d\n" % len(content)
print content
connection.commit()
connection.close()

This prints out the URL as an image! No idea why. But it does not produce
the desired image.

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 '\n'
  cursor.execute('select * from products where id = ' + str(d) +
';')
  col_fields = cursor.fetchall()
  if lang == 'es':
print 'ID: ', col_fields[0][0], ''
print 'Nombre: ', col_fields[0][2], ''
print 'Título: ', col_fields[0][6], ''
print 'Descripción: ', col_fields[0][9], ''
print 'Precio: ', col_fields[0][11], ''
print 'Recámaras: ', col_fields[0][12], ''
print 'Baños: ', col_fields[0][13], ''
content = col_fields[0][14].tostring()
print "Content-Type: image/jpeg\nContent-Length: %d\n" %
len(content)
print content, ''
...
  except:
if lang == 'es':
  print 'Lo siento. Todavía no tenemos propiedades para enseñarse en la
categoría de ', category, '.\n'



> You are NOT generating HTML, you are generating a JPEG image.


I am producing both, and this is causing me confusion.
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
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-09 Thread Steve Holden
Victor Subervi wrote:
> On Wed, Apr 9, 2008 at 1:14 AM, Gabriel Genellina 
> <[EMAIL PROTECTED] > wrote:
> 
>  > Thanks. I apparently am printing some holder for the image. I
> stripped
>  > out
>  > most of it with this
>  > content[0][0]
>  > but then I am left with this:
>  >
>  > array('c', '\xff\xd8\xff\xe0\\0\x10JFI...)
>  > How do I extract an image from that?
> 
> print content.tostring()
> 
> 
> Now *that* gave me something that *looks* a lot more like an image from 
> a programmers perspective, but still no image...
> ÿØÿàJFIF...
> Actually, it does not copy and paste well, but you can see it for the 
> moment here:
> http://livestocksling.com/test/python/Shop/display_es2.py
> So, how do I convert *that* to a real live image?
> 
> 
> Or perhaps, replace that line with content.tofile(sys.stdout)
> 
> 
> Printed nothing.
>  
> 
>  >> >   print 'Content-Type: image/jpeg\r\n'
>  >> >   print '\n'
>  >> >   print content
>  >> >   print '\n'
>  >> >   cursor.close()
>  >> >
>  >> > test()
> 
>  >> > The commented out line gives me a leading less than sign...and
> that´s
>  >> > it. What do?
> 
> Try to understand now *why* you got a single character with your
> previous
> code.
> 
> 
> No clue  :/
> 
> BTW, for purposes of documentation, it appears that, when sending the 
> form with the image to the script that processes the form, the following 
> is inadvisable:
> 
>   form = cgi.FieldStorage()
>   pic1 = form.getfirst('pic1', '')
> 
> This appears to work better:
> 
>   form = cgi.FieldStorage()
>   imgfile=open("pixel.gif",'rb')
>   pixel = imgfile.read()
>   pic1 = form.getfirst('pic1', pixel)
> 
> because it gives a binary default. The string appears to screw things up.
> Victor
> 
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".

You are NOT generating HTML, you are generating a JPEG image.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String Literal to Blob

2008-04-09 Thread Victor Subervi
On Wed, Apr 9, 2008 at 1:14 AM, Gabriel Genellina <[EMAIL PROTECTED]>
wrote:

> > Thanks. I apparently am printing some holder for the image. I stripped
> > out
> > most of it with this
> > content[0][0]
> > but then I am left with this:
> >
> > array('c', '\xff\xd8\xff\xe0\\0\x10JFI...)
> > How do I extract an image from that?
>
> print content.tostring()


Now *that* gave me something that *looks* a lot more like an image from a
programmers perspective, but still no image...
ÿØÿàJFIF...
Actually, it does not copy and paste well, but you can see it for the moment
here:
http://livestocksling.com/test/python/Shop/display_es2.py
So, how do I convert *that* to a real live image?


> Or perhaps, replace that line with content.tofile(sys.stdout)


Printed nothing.


> >> >   print 'Content-Type: image/jpeg\r\n'
> >> >   print '\n'
> >> >   print content
> >> >   print '\n'
> >> >   cursor.close()
> >> >
> >> > test()
>
> >> > The commented out line gives me a leading less than sign...and that´s
> >> > it. What do?
>
> Try to understand now *why* you got a single character with your previous
> code.


No clue  :/

BTW, for purposes of documentation, it appears that, when sending the form
with the image to the script that processes the form, the following is
inadvisable:

  form = cgi.FieldStorage()
  pic1 = form.getfirst('pic1', '')

This appears to work better:

  form = cgi.FieldStorage()
  imgfile=open("pixel.gif",'rb')
  pixel = imgfile.read()
  pic1 = form.getfirst('pic1', pixel)

because it gives a binary default. The string appears to screw things up.
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-08 Thread Gabriel Genellina
En Tue, 08 Apr 2008 13:55:07 -0300, Victor Subervi  
<[EMAIL PROTECTED]> escribió:

> Thanks. I apparently am printing some holder for the image. I stripped  
> out
> most of it with this
> content[0][0]

Yes, because of this:
   content = cursor.fetchall()
fetchall returns a list of rows, each row a tuple of columns.

> but then I am left with this:
>
> array('c', '\xff\xd8\xff\xe0\\0\x10JFI...)
> How do I extract an image from that?

print content.tostring()
Or perhaps, replace that line with content.tofile(sys.stdout)

http://docs.python.org/lib/module-array.html

>> > #  print 'Content-Type: image/jpeg\r\nContent-Length: %d\n' %
>> len(content)

Once you fix the former, you can re-enable that line.

>> >   print 'Content-Type: image/jpeg\r\n'
>> >   print '\n'
>> >   print content
>> >   print '\n'
>> >   cursor.close()
>> >
>> > test()

>> > The commented out line gives me a leading less than sign...and that´s
>> > it. What do?

Try to understand now *why* you got a single character with your previous  
code.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-08 Thread Victor Subervi
Thanks. I apparently am printing some holder for the image. I stripped out
most of it with this
content[0][0]
but then I am left with this:

array('c', '\xff\xd8\xff\xe0\\0\x10JFI...)
How do I extract an image from that?

TIA,

Victor



On Tue, Apr 8, 2008 at 11:15 AM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Victor Subervi wrote:
> > Hi:
> > I am able (finally) to upload an image to the database. However, when I
> > try to retrieve it, I get a string literal. Here is my code:
> >
> > #!/usr/local/bin/python
> > import cgitb; cgitb.enable()
> > import MySQLdb
> > def test():
> >   host = 'mysqldb2.ehost-services.com <
> http://mysqldb2.ehost-services.com>'
> >   user = 'user'
> >   passwd = 'pass'
> >   db = 'bre'
> >   db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
> >   cursor= db.cursor()
> >   cursor.execute('select pic1 from products where id="3";')
> >   content = cursor.fetchall()
> > #  print 'Content-Type: image/jpeg\r\nContent-Length: %d\n' %
> len(content)
> >   print 'Content-Type: image/jpeg\r\n'
> >   print '\n'
> >   print content
> >   print '\n'
> >   cursor.close()
> >
> > test()
> > (Apparently, Plesk doesn´t like if __name__ == '__main__': )
> > The commented out line gives me a leading less than sign...and that´s
> > it. What do?
> > TIA,
> > Victor
> >
> Your headers indicate you intend to serve a JPEG image, so you should
> *not* then include HTML. Take a look at the HTML of a web page with an
> image inside it (look for the  tag) and you will see that
> HTML pages reference images as separate web resources.
>
> Thus once you have printed out your HTML headers you should them
> immediately send the contents of the database column.
>
> regards
>  Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: String Literal to Blob

2008-04-08 Thread Steve Holden
Victor Subervi wrote:
> Hi:
> I am able (finally) to upload an image to the database. However, when I 
> try to retrieve it, I get a string literal. Here is my code:
>  
> #!/usr/local/bin/python
> import cgitb; cgitb.enable()
> import MySQLdb
> def test():
>   host = 'mysqldb2.ehost-services.com '
>   user = 'user'
>   passwd = 'pass'
>   db = 'bre'
>   db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
>   cursor= db.cursor()
>   cursor.execute('select pic1 from products where id="3";')
>   content = cursor.fetchall()
> #  print 'Content-Type: image/jpeg\r\nContent-Length: %d\n' % len(content)
>   print 'Content-Type: image/jpeg\r\n'
>   print '\n'
>   print content
>   print '\n'
>   cursor.close()
>  
> test()
> (Apparently, Plesk doesn´t like if __name__ == '__main__': )
> The commented out line gives me a leading less than sign...and that´s 
> it. What do?
> TIA,
> Victor
> 
Your headers indicate you intend to serve a JPEG image, so you should 
*not* then include HTML. Take a look at the HTML of a web page with an 
image inside it (look for the  tag) and you will see that 
HTML pages reference images as separate web resources.

Thus once you have printed out your HTML headers you should them 
immediately send the contents of the database column.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


String Literal to Blob

2008-04-08 Thread Victor Subervi
Hi:
I am able (finally) to upload an image to the database. However, when I try
to retrieve it, I get a string literal. Here is my code:

#!/usr/local/bin/python
import cgitb; cgitb.enable()
import MySQLdb
def test():
  host = 'mysqldb2.ehost-services.com'
  user = 'user'
  passwd = 'pass'
  db = 'bre'
  db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
  cursor= db.cursor()
  cursor.execute('select pic1 from products where id="3";')
  content = cursor.fetchall()
#  print 'Content-Type: image/jpeg\r\nContent-Length: %d\n' % len(content)
  print 'Content-Type: image/jpeg\r\n'
  print '\n'
  print content
  print '\n'
  cursor.close()

test()
(Apparently, Plesk doesn´t like if __name__ == '__main__': )
The commented out line gives me a leading less than sign...and that´s it.
What do?
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list