This article mainly introduces the socket Port Redirection example implemented by python. the implementation function is to enable file sharing on port 12345, at this time, I also want to access this sharing through port 8000 at the same time, which can be easily enabled on port 12345, the effect is as follows:
To implement the desired function, you only need to redirect the port. the code is as follows:
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 ', 192 ))
Sock1.listen (5)
Sock2 = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Sock2.connect ('192. 168.168.100 ', 192 ))
While True:
Connection, address = sock1.accept ()
Buf = connection. recv (bufLen)
# Print buf
Sock2.send (buf)
Connection. send (sock2.recv (bufLen ))
Connection. close ()
Running effect: