Replacing your user agent with a Twisted proxy

A couple of days ago, a friend sent me a link to Google's "killer-robots.txt" easter-egg. I was curious to see if there was more to it than just the file (a tribute to both the robots.txt file and to the Terminator movies), and started trying to mess around with the user agent I was sending to I had started by using Burp Suite, which allows you to intercept and alter your requests, but it was a drag having to repeatedly intercept and alter the User-Agent string; consequently, I ended up writing the python code below.

It's the first time I use Twisted to do anything - I have to say, I like its simplicity.

Like the header says, I did end up using a Firefox plugin to change the user agent; but I thought that the code would be useful to have around for those of you that might wish to intercept and alter a whole bunch of packets. I use regex to identify the user-agent string and replace it with whatever I want - this method grants you quite a bit of flexibility in finding and replacing elements of your request.