Python implements the socket port redirection example

  • 2020-04-02 13:24:14
  • OfStack

Sharing can be easily opened on port 12345, the effect is as follows:

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201402/20140210111151.jpg? 2014110111333 ">

To achieve the function I want, I just need to redirect the port. The code is as follows:


#! /usr/bin/python
'''
      File      : redirect.py
      Author    : Mike
'''
import socket,os
bufLen = 4*1024
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
sock1.bind(('192.168.168.100', 8000))  
sock1.listen(5)  
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
sock2.connect(('192.168.168.100', 12345))  
while True:
        connection,address = sock1.accept()  
        buf = connection.recv(bufLen)  
        #print buf            
        sock2.send(buf)  
        connection.send(sock2.recv(bufLen))
        connection.close()

Operation effect:

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201402/20140210111248.jpg? 2014110111355 ">


Related articles: