Skip to main content

Python: Web Beacons in Pylons

A Web beacon is usually an image tag that refers to a 1x1 clear gif on a remote server. The remote server is able to track that the gif was seen when the browser tries to download it. If you're using Pylons, here's how to implement that beacon in a way that won't be cached:
CLEAR_GIF = 'GIF89a\x01\x00\x01\x00\x91\xff\x00\xff\xff\xff\x00\x00\x00\xff\xff\xff\x00\x00\x00!\xff\x0bADOBE:IR1.0\x02\xde\xed\x00!\xf9\x04\x01\x00\x00\x02\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02T\x01\x00;'
...
def some_action(self):
# Do interesting things here...
response.headers['Content-Type'] = 'image/gif'
response.headers['Cache-Control'] = 'no-cache'
response.write(CLEAR_GIF)

Comments

Hmm, it's truncating the CLEAR_GIF. Click on the comments page and click "Show Original Post". Alternatively, look at the HTML source for the page. Sorry for the inconvenience.
John said…
I think that the no-cache header is intended as an instruction to caching proxies and not to User Agents/Browsers. For this you should probably be setting the Expires header instead.
You may be right.

I did what I did because the book "RESTful Web Services" gave an overview on caching and said to do it that way.