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)文章
一文掌握6種Python中常用數(shù)據(jù)庫操作及代碼
在數(shù)據(jù)處理和管理領(lǐng)域,Python作為一種高效、易用的編程語言,擁有豐富的數(shù)據(jù)庫操作模塊,可以輕松實現(xiàn)對關(guān)系型數(shù)據(jù)庫的數(shù)據(jù)操作,本文將介紹六種常見的Python數(shù)據(jù)庫操作模塊,需要的可以參考下2023-12-12
Python?pandas修剪函數(shù)clip使用實例探究
在數(shù)據(jù)處理和分析中,經(jīng)常面臨著需要限制數(shù)據(jù)范圍的情況,而pandas庫提供的clip函數(shù)就是一個強大的工具,可以方便地對數(shù)據(jù)進行修剪,本文將深入介紹clip函數(shù)的基本用法、常見參數(shù)以及實際場景中的應(yīng)用,以幫助大家充分理解并靈活運用這一功能2024-01-01
Tensorflow2.4使用Tuner選擇模型最佳超參詳解
這篇文章主要介紹了Tensorflow2.4使用Tuner選擇模型最佳超參詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Python實現(xiàn)線程狀態(tài)監(jiān)測簡單示例
這篇文章主要介紹了Python實現(xiàn)線程狀態(tài)監(jiān)測,結(jié)合簡單實例形式分析了Python線程start啟動、sleep推遲運行、isAlive判斷等方法使用技巧,需要的朋友可以參考下2018-03-03

