>So the program uses the socket module and threading module. it is similar to a technique found here Python Network Programming. Another resource can be found at Tutorial on Threads. I cannot remember where I first read this, these small thread servers seem a little bit excessive but they are actually a great introduction to threads.
#!/usr/bin/env python """ Python Proxy ver 0.01 last editted 4/6/2008 """ import socket as S import threading #this is a socket class as a thread. class Listener(threading.Thread): def boot(self,name): self.sock = S.socket() self.running = 0 self.partner = S.socket() self.name=name def run(self): self.running = 1 while(self.running==1): data = self.sock.recv(1024) if(len(data)>1): data2 = data.replace(ProxyPort, callPort) data2 = data2.replace(ProxyAddress,callAddress) self.partner.sock.send(data2) print self.name print repr(data) def end(self): self.sock.close() self.running = 0 #setup variables. This is the address #your computer uses to connect to. (what you put in your browser). ProxyAddress = 'localhost' ProxyPort = '8000' callAddress = 'www.google.com' callPort = '80' myHost = S.socket() myHost.bind(('localhost',int(ProxyPort))) myHost.listen(5) goBetween = Listener() goBetween.boot('Local Client') (goBetween.sock,gbAddress) = myHost.accept() caller = Listener() caller.boot('Desired Service') caller.sock.connect((callAddress,int(callPort))) goBetween.partner = caller caller.partner = goBetween goBetween.start() caller.start() text = raw_input('functioning to here') myHost.close() goBetween.end() caller.end()
Currently there are a couple issues, if you close out sometimes it locks up. I think sockets have timeouts that I should set.