Example of port forwarding and redirection under TCP/IP protocol in Python

Source: Internet
Author: User
This article mainly introduces the example of port forwarding and redirection implemented by Python under the TCPIP protocol. it is demonstrated by a webpy site with two-way communication ports on the local machine. if you need a friend, refer to the following, we use webpy to write a simple website, listen to port 8080, and return the "Hello, EverET.org" page.

Then we use our forwarding. py to establish two communication pipelines between Port 80 and port 8080 for bidirectional communication.

At this point, we access our server through port 80.

The browser obtains:

Then, we can see the communication content between the browser and webpy in the output result of 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(1024)        log(data)        if not data: break        self.target.send(data)      except:        break    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))    self.sock.listen(10)  def run(self):    while True:      client_fd, client_addr = self.sock.accept()      target_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)      target_fd.connect((self.targethost, self.targetport))      log('new connect')      # two 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()

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.