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!

tagged: python» react

Comments

Add a comment:

name
email
Ignore this:
not displayed and no spam.
Leave this empty:
www
not searched.
 
Name and either email or www are required.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.