Ny command-line, generic HTTP proxy (like Squid)

command linehttpPROXYsquid

I can easily use Netcat (or, Socat) to capture traffic between my browser and a specific host:port.

But for Linux, does there exist any command-line counterpart of a Squid-like HTTP proxy that I can use to capture traffic between my HTTP client (either browser or command-line program) and any arbitrary host:port?

Best Answer

Both Perl and Python (and probably Ruby as well) have simple kits that you can use to quickly build simple HTTP proxies.

In Perl, use HTTP::Proxy. Here's the 3-line example from the documentation. Add filters to filter, log or rewrite requests or responses; see the documentation for examples.

use HTTP::Proxy;
my $proxy = HTTP::Proxy->new( port => 3128 );
$proxy->start;

In Python, use SimpleHTTPServer. Here's some sample code lightly adapted from effbot. Adapt the do_GET method (or others) to filter, log or rewrite requests or responses.

import SocketServer
import SimpleHTTPServer
import urllib
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.copyfile(urllib.urlopen(self.path), self.wfile)
httpd = SocketServer.ForkingTCPServer(('', 3128), Proxy)
httpd.serve_forever()
Related Question