First, we use Webpy to write a simple website, listen to 8080 ports, and return to the "Hello, everet.org" page.
Then we use our forwarding.py to build two communication pipelines between the 80 port and 8080 ports for two-way communication.
At this point, we have access to our servers via port 80.
The browser gets:
We can then see the communication between the browser and the webpy in the output of the forwarding.py.
Code:
#!/usr/bin/env pythonimport sys, socket, time, Threadingloglock = threading. Lock () def log (msg): Loglock.acquire () Try:print ' [%s]: \n%s\n '% (Time.ctime (), Msg.strip ()) Sys.stdout.flush () Finally:loglock.release () class Pipethread (threading. Thread): Def __init__ (self, source, target): threading. Thread.__init__ (self) self.source = Source Self.target = Target def run (self): while True:try:data = SELF.SOURCE.RECV (1024x768) log (data) if not data:break self.target.send (data) Except:bre AK log (' Pipethread done ') class Forwarding (threading. Thread): Def __init__ (self, port, Targethost, Targetport): Threading. Thread.__init__ (self) self.targethost = targethost Self.targetport = Targetport Self.sock = socket.socket (socket. Af_inet, Socket. Sock_stream) Self.sock.bind ((' 0.0.0.0 ', Port)) def run (self): while TRUE:CLIENT_FD, C LIENT_ADDR = self.sock.accept () TARGET_FD = socket. Socket (socket.af_inet, socket. Sock_stream) Target_fd.connect ((Self.targethost, Self.targetport)) log (' New Connect ') # both direct pipe Pipethread (TARGET_FD, CLIENT_FD). Start () pipethread (CLIENT_FD, TARGET_FD). Start () if __name__ = = ' __main__ ': print ' Starting ' import sys try:port = int (sys.argv[1]) Targethost = sys.argv[2] try:targetport = Int (sys.argv[3]) Except Indexerror:targetport = port except (ValueError, indexerror): print ' Usage:%s port targethost [Targetport] '% sys.argv[0] sys.exit (1) #sys. StdOut = open (' Forwaring.log ', ' W ') Forwarding (port, Targethost, Targetport). Start ()