python实现超简单端⼝转发的⽅法本⽂实例讲述了python实现超简单端⼝转发的⽅法。分享给⼤家供⼤家参考。具体如下:
代码⾮常简单,实现了简单的端⼝数据转发功能,⽤于真实环境还需要再修改⼀下。
复制代码代码如下:
#tcp server
import socket
host = '127.0.0.1'          #Local Server IPrecv函数
host2 = '127.0.0.1'  #Real Server IP
port = 6001 #Local Server Port
port2 = 7001 #Real Server Port
def ProcData(data):
return data
#add
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(('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 )
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)
v(20480)
#print "Client recv data %s from "%repr(buf)
ss.send(buf)
print "Send:"+repr(buf)+"\r\n"
希望本⽂所述对⼤家的Python程序设计有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。