Python: batteries included

Wednesday 13 March 2002This is almost 23 years old. Be careful.

When Python claims that batteries are included, they mean that Python has a very rich standard library.

Today I had a vivid demonstration. I was sick of seeing ads in my Yahoo mail, so I did the hosts file hack: enter the ad servers’ names as 127.0.0.1, and the images can’t be served. It worked: instead of ad images, I had broken images. They’re not pretty, but at least they aren’t ads.

Then I figured, “if I had a simple web server running on this machine, I could serve my own gif in place of those ads, and I wouldn’t have broken images”.

I did a little poking around in the Python manuals, and 8 lines later, I was done:

import BaseHTTPServer, SimpleHTTPServer

class BogusAdRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def translate_path(self, path):
        return r'c:\img\1green.gif'

httpd = BaseHTTPServer.HTTPServer(('', 80), BogusAdRequestHandler)
httpd.serve_forever()

This is a web server which returns a one-pixel green gif no matter what file was requested. I run this on my machine, and now I have nice clean pages with no ads and no broken images, and a pleasing green field where the ads used to be!

Comments

[gravatar]
Hi Ned,

Although you wrote this over 12 years ago I just implemented it on my home network. Instead of running it on my windows laptop I created a daemon on my Raspberry Pi which also serves DHCP and DNS with dnsmasq. That way, all clients on my network can benefit from the ad blocker.

Great idea, thanks!

Chris (also ex DEC, now at IDC)
[gravatar]
After using this for more than a year it appears that doubleclick and others do something other than a simple image link. This causes the browser to wait for a minute or two before timing out. I've not been able to figure out a way around it, although admittedly I've not spent a lot of time looking into it.

Chris

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.