Wednesday 13 March 2002 — This is over 22 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
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)
Chris
Add a comment: