Python實(shí)現(xiàn)TCP協(xié)議下的端口映射功能的腳本程序示例
1 端口映射
舉個(gè)例子來(lái)說(shuō)明一下端口映射的作用。
有A、B、C三臺(tái)計(jì)算機(jī),A、B互通,B、C互通,但是A、C不通,這個(gè)時(shí)候在C上開(kāi)了一個(gè)Web服務(wù),如何讓A訪問(wèn)C的Web服務(wù)?
最簡(jiǎn)單有效的辦法就是在B上開(kāi)一個(gè)端口映射服務(wù),然后讓A訪問(wèn)B的某個(gè)端口,B將這個(gè)端口上的所有流量全部轉(zhuǎn)發(fā)到C的Web服務(wù)端口上,同時(shí)將C上Web服務(wù)返回的流量也全部轉(zhuǎn)發(fā)給A。這樣對(duì)A來(lái)說(shuō),以B為跳板,實(shí)現(xiàn)了間接訪問(wèn)C上Web服務(wù)的目的。
2 實(shí)現(xiàn)流程
端口映射的原理并不復(fù)雜,本文以TCP為例介紹一下實(shí)現(xiàn)過(guò)程,簡(jiǎn)單畫了個(gè)時(shí)序圖(如下),這里就不再用文字贅述了。
需要注意的是,由于端口映射只是單純的流量轉(zhuǎn)發(fā),對(duì)應(yīng)用層數(shù)據(jù)不進(jìn)行處理,所以對(duì)于多通道協(xié)議是無(wú)法支持的(如FTP協(xié)議)。
3 代碼示例
按照上面的流程,Python實(shí)現(xiàn)如下(建議從后向前看):
# -*- coding: utf-8 -*- # tcp mapping created by hutaow(hutaow.com) at 2014-08-31 import socket import threading # 端口映射配置信息 CFG_REMOTE_IP = '192.168.0.10' CFG_REMOTE_PORT = 22 CFG_LOCAL_IP = '0.0.0.0' CFG_LOCAL_PORT = 10022 # 接收數(shù)據(jù)緩存大小 PKT_BUFF_SIZE = 2048 # 調(diào)試日志封裝 def send_log(content): print content return # 單向流數(shù)據(jù)傳遞 def tcp_mapping_worker(conn_receiver, conn_sender): while True: try: data = conn_receiver.recv(PKT_BUFF_SIZE) except Exception: send_log('Event: Connection closed.') break if not data: send_log('Info: No more data is received.') break try: conn_sender.sendall(data) except Exception: send_log('Error: Failed sending data.') break # send_log('Info: Mapping data > %s ' % repr(data)) send_log('Info: Mapping > %s -> %s > %d bytes.' % (conn_receiver.getpeername(), conn_sender.getpeername(), len(data))) conn_receiver.close() conn_sender.close() return # 端口映射請(qǐng)求處理 def tcp_mapping_request(local_conn, remote_ip, remote_port): remote_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: remote_conn.connect((remote_ip, remote_port)) except Exception: local_conn.close() send_log('Error: Unable to connect to the remote server.') return threading.Thread(target=tcp_mapping_worker, args=(local_conn, remote_conn)).start() threading.Thread(target=tcp_mapping_worker, args=(remote_conn, local_conn)).start() return # 端口映射函數(shù) def tcp_mapping(remote_ip, remote_port, local_ip, local_port): local_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) local_server.bind((local_ip, local_port)) local_server.listen(5) send_log('Event: Starting mapping service on ' + local_ip + ':' + str(local_port) + ' ...') while True: try: (local_conn, local_addr) = local_server.accept() except KeyboardInterrupt, Exception: local_server.close() send_log('Event: Stop mapping service.') break threading.Thread(target=tcp_mapping_request, args=(local_conn, remote_ip, remote_port)).start() send_log('Event: Receive mapping request from %s:%d.' % local_addr) return # 主函數(shù) if __name__ == '__main__': tcp_mapping(CFG_REMOTE_IP, CFG_REMOTE_PORT, CFG_LOCAL_IP, CFG_LOCAL_PORT)
4 運(yùn)行
運(yùn)行效果如下,192.168.0.20通過(guò)連接映射服務(wù)器的10022端口,成功訪問(wèn)192.168.0.10的SSH服務(wù)(22端口):
- python matplotlib imshow熱圖坐標(biāo)替換/映射實(shí)例
- python 實(shí)現(xiàn)12bit灰度圖像映射到8bit顯示的方法
- Python匿名函數(shù)/排序函數(shù)/過(guò)濾函數(shù)/映射函數(shù)/遞歸/二分法
- 詳解python-圖像處理(映射變換)
- Python字典中的鍵映射多個(gè)值的方法(列表或者集合)
- Python簡(jiǎn)單實(shí)現(xiàn)的代理服務(wù)器端口映射功能示例
- 詳解Python中映射類型的內(nèi)建函數(shù)和工廠函數(shù)
- Python3 mmap內(nèi)存映射文件示例解析
相關(guān)文章
python設(shè)置環(huán)境變量的作用和實(shí)例
在本篇文章里小編給各位整理了關(guān)于python設(shè)置環(huán)境變量的作用和實(shí)例內(nèi)容知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)參考下。2019-07-07關(guān)于Python八大排序?qū)崿F(xiàn)方法(冒泡排序、快速排序等)
這篇文章主要介紹了關(guān)于Python八大排序?qū)崿F(xiàn)方法,主要有基數(shù)排序、歸并排序、堆排序、簡(jiǎn)單選擇排序、直接插入排序、希爾排序、快速排序、冒泡排序等,需要的朋友可以參考下2023-03-03Python使用requests發(fā)送POST請(qǐng)求實(shí)例代碼
這篇文章主要介紹了Python使用requests發(fā)送POST請(qǐng)求實(shí)例代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01python過(guò)濾中英文標(biāo)點(diǎn)符號(hào)的實(shí)例代碼
今天小編就為大家分享一篇python過(guò)濾中英文標(biāo)點(diǎn)符號(hào)的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07動(dòng)態(tài)創(chuàng)建類實(shí)例代碼
Python中要?jiǎng)?chuàng)建一個(gè)類的實(shí)例,要首先導(dǎo)入該類或者該類所屬的模塊。2009-10-10python基礎(chǔ)之文件處理知識(shí)總結(jié)
今天帶大家了解python文件處理的相關(guān)知識(shí),文中介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05利用python list完成最簡(jiǎn)單的DB連接池方法
這篇文章主要介紹了利用python list完成最簡(jiǎn)單的DB連接池方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08pandas創(chuàng)建DataFrame對(duì)象失敗的解決方法
本文主要介紹了pandas創(chuàng)建DataFrame對(duì)象失敗的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01一些讓Python代碼簡(jiǎn)潔的實(shí)用技巧總結(jié)
隨著項(xiàng)目代碼行數(shù)的增加,不可避免的遇到軟件架構(gòu)腐敗的問(wèn)題,所以如何寫出簡(jiǎn)潔的代碼至關(guān)重要,這篇文章主要給大家介紹了一些讓Python代碼簡(jiǎn)潔的實(shí)用技巧,需要的朋友可以參考下2021-08-08