Forcing a socket connection via a proxy

Fuzzyman michael at foord.net
Wed Jul 28 16:38:55 CEST 2004


I'm trying to create a proxy server - one that will modify requests
made through it.

I've started with Tiny HTTP Proxy by SUZUKI Hisao which is built on
BaseHTTPServer - and I'm starting to get somewhere.

It listens on local ports and tells you whats goign through it.
I'm in a slightly unusual situation though - all internet traffic goes
through a proxy - I have to set the proxy settings to dav-serv:8080 to
work.

What I *want* to do is to point my browser at 127.0.0.1:8000 and have
Tiny Proxy pointed at dav-serv and also modifying requests made
through it. However - when I change my browser proxy settings to
127.0.0.1, the python code no longer knows that it needs to go via
dav-serv:8080

What I need to do is to force it to use our proxy. Now I know various
protocols allow you to explicitly state the proxy settings... but I'm
struggling to find it.

As far as I can tell Tiny Proxy creates a ProxyHandler with methods
that actually implement the fetches.

For example there are two methods in the ProxyHandler class (which is
a subclass of BaseHTTPServer.BaseHTTPRequestHandler) :


    def do_CONNECT(self):
        soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            if self._connect_to(self.path, soc):
                self.log_request(200)
                self.wfile.write(self.protocol_version +
                                 " 200 Connection established\r\n")
                self.wfile.write("Proxy-agent: %s\r\n" %
self.version_string())
                self.wfile.write("\r\n")
                self._read_write(soc, 300)
        finally:
            print "\t" "bye"
            soc.close()
            self.connection.close()

    def do_GET(self):
        (scm, netloc, path, params, query, fragment) =
urlparse.urlparse(
            self.path, 'http')
        if scm != 'http' or fragment or not netloc:
            self.send_error(400, "bad url %s" % self.path)
            return
        soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Which look like they implement GET and CONNECT requests (good at this
aren't I !! - actually I'm way out of my depth). They both get the URL
from self.path and use socket.socket to make connection. I need to get
this connection to be made via a proxy server. (I also want to modify
that URL - but I can handle that myself !!).

Anyone got any pointers for me ?

Regards,

Fuzzyman

http://www.voidspace.org.uk/atlantibots/pythonutils.html



More information about the Python-list mailing list