圆月山庄资源网 Design By www.vgjia.com

本文实例讲述了python实现超简单端口转发的方法。分享给大家供大家参考。具体如下:

代码非常简单,实现了简单的端口数据转发功能,用于真实环境还需要再修改一下。
复制代码 代码如下:#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(('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"

希望本文所述对大家的Python程序设计有所帮助。

标签:
python,超简单,端口转发,方法

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com