python實(shí)現(xiàn)超簡單端口轉(zhuǎn)發(fā)的方法
本文實(shí)例講述了python實(shí)現(xiàn)超簡單端口轉(zhuǎn)發(fā)的方法。分享給大家供大家參考。具體如下:
代碼非常簡單,實(shí)現(xiàn)了簡單的端口數(shù)據(jù)轉(zhuǎn)發(fā)功能,用于真實(shí)環(huá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è)計(jì)有所幫助。
相關(guān)文章
以windows service方式運(yùn)行Python程序的方法
這篇文章主要介紹了以windows service方式運(yùn)行Python程序的方法,可實(shí)現(xiàn)將Python程序變成windows服務(wù)的功能,需要的朋友可以參考下2015-06-06python實(shí)現(xiàn)定時自動備份文件到其他主機(jī)的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)定時自動備份文件到其他主機(jī)的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-02-02淺談python中copy和deepcopy中的區(qū)別
Python學(xué)習(xí)過程中會遇到許多問題,最近對copy和deepcopy略感困惑,下面對其進(jìn)行解答,需要的朋友可以參考。2017-10-10django使用多個數(shù)據(jù)庫的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于django使用多個數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03