Database Reference
In-Depth Information
if
row
is
not
None
:
(
type
,
data
)
=
row
# Send image to client, preceded by Content-Type: and
# Content-Length: headers. The Expires:, Cache-Control:, and
# Pragma: headers help keep browsers from caching the image
# and reusing it for successive requests for this script.
print
(
"Content-Type:
%s
"
%
type
)
print
(
"Content-Length:
%s
"
%
len
(
data
))
print
(
"Expires: Sat, 01 Jan 2000 00:00:00 GMT"
)
print
(
"Cache-Control: no-cache"
)
print
(
"Pragma: no-cache"
)
print
(
""
)
print
(
data
)
conn
.
close
()
banner.py
sends a few headers in addition to the usual
Content-Type:
and
Content-
Length:
headers. The extra headers help keep browsers from caching the image.
Ex
pires:
specifies a date in the past to tell the browser that the image is out of date. The
Cache-Control:
and
Pragma:
headers tell the browser not to cache the image. The script
sends both headers because some browsers understand one, and some the other.
Why suppress caching? Because if you don't, the browser will send a request for
ban‐
ner.py
only the first time it sees it in a link. On subsequent requests for the script, the
browser will reuse the image, which defeats the intent of having each such link resolve
to a randomly chosen image.
Install the
banner.py
script in your
cgi-bin
directory. Then, to place a banner in a web
page, use an
<img>
tag that invokes the script. For example, if the script is installed as
/
cgi-bin/banner.py
, the following page references it to include an image below the in‐
troductory paragraph:
<!-- bannertest1.html: page with single link to banner-ad script -->
<html>
<head><title>
Banner Ad Test Page 1
</title></head>
<body>
<p>
You should see an image below this paragraph.
</p>
<img
src=
"/cgi-bin/banner.py"
/>
</body>
</html>
If you request this page, it should display an image, and you should see a succession of
randomly chosen images each time you reload the page. (I am assuming here that you
have loaded several images into the
image
table by now using the
store_image.pl
script
discussed in
Recipe 19.6
. Otherwise you'll see no images at all!) If you modify
ban‐
ner.py
not to send the cache-related headers, you likely will see the same image each
time you reload the page.