Making a socket connection via a proxy server

Fuzzyman michael at foord.net
Fri Jul 30 08:33:04 EDT 2004


In a nutshell - the question I'm asking is, how do I make a socket
conenction go via a proxy server ?
All our internet traffic has to go through a proxy-server at location
'dav-serv:8080' and I need to make a socket connection through it.

The reason (with code example) is as follows :

I am hacking "Tiny HTTP Proxy" by SUZUKI Hisao to make an http proxy
that modifies URLs. I haven't got very far - having started from zero
knowledge of the 'hyper text transfer protocol'.

It looks like the Tiny HTTP Proxy (using BaseHTTPServer as it's
foundation) intercepts all requests to local addresses and then
re-implements the request (whether it is CONNECT, GET, PUT or
whatever). It logs everything that goes through it - I will simply
edit it to amend the URL that is being asked for.

It looks like the CONNECT and GET requests are just implemented using
simple socket commands. (I say simple because there isn't a lot of
code - I'm not familiar with the actual behaviour of sockets, but it
doesn't look too complicated).

What I need to do is rewrite the soc.connect(host_port) line in the
following example so that it connects *via* my proxy-server. (which it
doesn't by default).

I think the current format of host_port is a tuple : (host_domain,
port_no)

Below is a summary of the GET command (I've inlined all the method
calls - this example starts from the do_GET method) :

soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect(host_port)
soc.send("%s %s %s\r\n" % (
    self.command,
    urlparse.urlunparse(('', '', path, params, query, '')),
    self.request_version))
self.headers['Connection'] = 'close'
del self.headers['Proxy-Connection']
for key_val in self.headers.items():
    soc.send("%s: %s\r\n" % key_val)
soc.send("\r\n")
    
max_idling=20                                       # this is really
part of a self._read_write method
iw = [self.connection, soc]
ow = []
count = 0
while 1:
    count += 1
    (ins, _, exs) = select.select(iw, ow, iw, 3)
    if exs: break
    if ins:
        for i in ins:
            if i is soc:
                out = self.connection
            else:
                out = soc
            data = i.recv(8192)
            if data:
                out.send(data)
                count = 0
    else:
        print "\t" "idle", count
    if count == max_idling: break
    
print "\t" "bye"
soc.close()
self.connection.close()

Regards,


Fuzzy

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



More information about the Python-list mailing list