The example in this article describes Python's method for implementing super simple port forwarding. Share to everyone for your reference. Specifically as follows:
The code is very simple, the implementation of a simple port data forwarding function, for the real environment also need to be modified.
Copy Code code 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" + Host + ":" + str (port) + "to" + Host2 + ":" + str (PORT2) + "\ r \ n"
Server = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Server.bind (' 127.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 your Python programming.