String Literal to Blob

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Apr 10 22:01:41 EDT 2008


En Thu, 10 Apr 2008 14:04:43 -0300, Victor Subervi  
<victorsubervi at gmail.com> 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 '<img src="%s"><br /><br />' % 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:

<html><body>
<p>This is me: <img src="/images/myself.jpg">
and this is my cat <img src="/images/garfield.jpg">
</body></html>

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




More information about the Python-list mailing list