This article describes how to implement ultra-simple port forwarding in python. The example analyzes the port forwarding technique of Python homogeneous socket, which has some reference value, for more information about how to implement ultra-simple port forwarding in python, see the example in this article. Share it with you for your reference. The details are as follows:
The code is very simple and implements a simple port data forwarding function. you need to modify it for the real environment.
The code is as follows:
# Tcp server
Import socket
Host = '127. 0.0.1 '# Local Server IP
Host2 = '127. 0.0.1 '# Real Server IP
Port = 6001 # Local Server Port
Port2 = 7001 # Real Server Port
Def ProcData (data ):
Return data
# Add more code ....
Print "Map Server start from" + host + ":" + str (port) + "to" + host2 + ":" + str (port2) + "\ r \ n"
Server = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Server. bind ('2014. 0.0.1 ', port ))
Print "127.0.0.1 Server start at" + str (port) + "\ r \ n"
Client = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Client. connect (host2, port2 ))
Print host + "Client connect to" + host2 + ":" + str (port2) + "\ n"
Server. listen (5)
Ss, addr = server. accept ()
Print 'got connected from', addr
While 1:
Msg = ss. recv (20480)
Print "Get:" + repr (msg) + "\ r \ n"
Client. send (msg)
# Print "Client send data % s to" % repr (msg)
Buf = client. recv (20480)
# Print "Client recv data % s from" % repr (buf)
Ss. send (buf)
Print "Send:" + repr (buf) + "\ r \ n"
I hope this article will help you with Python programming.