Making a socket connection via a proxy server

From: Fuzzyman (michael_at_foord.net)
Date: 07/30/04


Date: 30 Jul 2004 05:33:04 -0700

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



Relevant Pages

  • Re: EOF, close(), shutdown(), on POSIX sockets
    ... I'm working on the core of a tcp proxy application. ... server to send that request to, forks a child, the child connects to the ... and drops the client and server socket into the core ... If I receive EOF when I read from A, I think I should do shutdown(B, ...
    (comp.os.linux.development.apps)
  • Re: proxy does not work with secure sites
    ... > coonected to port 80 i get this excpetion some times.. ... It must open a socket to the target server ... the proxy should simply forward ... >>> not serving the request.. ...
    (microsoft.public.win32.programmer.networks)
  • Sockets help
    ... a very simple socket function, client and server. ... through a proxy in between. ...
    (comp.lang.tcl)
  • Re: Splitting a full-duplex socket
    ... to circumvent this limitation you create a HTTP proxy which takes this ... create a normal socket on the proxy etc. ... But you kept saying you had no Full-duplex connection, ...
    (comp.lang.java.programmer)
  • Re: Cannot access any Microsoft pages or some secure sites
    ... ISP, or the same physical telephone line at the same location? ... If the former, then it could be a proxy that the ISP is running, and my ... Not going thru a router, only using a cable modem. ... the outgoing HTTP requests. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)