python實現(xiàn)超簡單端口轉(zhuǎn)發(fā)的方法
更新時間:2015年03月13日 14:54:51 作者:chongq
這篇文章主要介紹了python實現(xiàn)超簡單端口轉(zhuǎn)發(fā)的方法,實例分析了Python同構(gòu)socket實現(xiàn)端口轉(zhuǎn)發(fā)的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了python實現(xiàn)超簡單端口轉(zhuǎn)發(fā)的方法。分享給大家供大家參考。具體如下:
代碼非常簡單,實現(xiàn)了簡單的端口數(shù)據(jù)轉(zhuǎn)發(fā)功能,用于真實環(huán)境還需要再修改一下。
復(fù)制代碼 代碼如下:
#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"
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程序設(shè)計有所幫助。
相關(guān)文章
以windows service方式運行Python程序的方法
這篇文章主要介紹了以windows service方式運行Python程序的方法,可實現(xiàn)將Python程序變成windows服務(wù)的功能,需要的朋友可以參考下2015-06-06python實現(xiàn)定時自動備份文件到其他主機的實例代碼
這篇文章主要介紹了python實現(xiàn)定時自動備份文件到其他主機的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-02-02淺談python中copy和deepcopy中的區(qū)別
Python學(xué)習(xí)過程中會遇到許多問題,最近對copy和deepcopy略感困惑,下面對其進行解答,需要的朋友可以參考。2017-10-10