Python implements port forwarding and redirection examples under TCP/IP protocol

Source: Internet
Author: User
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 ()
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.