Python實(shí)現(xiàn)TCP/IP協(xié)議下的端口轉(zhuǎn)發(fā)及重定向示例
首先,我們用webpy寫一個(gè)簡(jiǎn)單的網(wǎng)站,監(jiān)聽(tīng)8080端口,返回“Hello, EverET.org”的頁(yè)面。
然后我們使用我們的forwarding.py,在80端口和8080端口中間建立兩條通信管道用于雙向通信。
此時(shí),我們通過(guò)80端口訪問(wèn)我們的服務(wù)器。
瀏覽器得到:
然后,我們?cè)趂orwarding.py的輸出結(jié)果中可以看到瀏覽器和webpy之間的通信內(nèi)容。
代碼:
#!/usr/bin/env python import sys, socket, time, threading loglock = threading.Lock() def log(msg): loglock.acquire() try: print '[%s]: \n%s\n' % (time.ctime(), msg.strip()) sys.stdout.flush() finally: loglock.release() class PipeThread(threading.Thread): def __init__(self, source, target): threading.Thread.__init__(self) self.source = source self.target = target def run(self): while True: try: data = self.source.recv(1024) log(data) if not data: break self.target.send(data) except: break log('PipeThread done') class Forwarding(threading.Thread): def __init__(self, port, targethost, targetport): threading.Thread.__init__(self) self.targethost = targethost self.targetport = targetport self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind(('0.0.0.0', port)) self.sock.listen(10) def run(self): while True: client_fd, client_addr = self.sock.accept() target_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM) target_fd.connect((self.targethost, self.targetport)) log('new connect') # two direct pipe PipeThread(target_fd, client_fd).start() PipeThread(client_fd, target_fd).start() if __name__ == '__main__': print 'Starting' import sys try: port = int(sys.argv[1]) targethost = sys.argv[2] try: targetport = int(sys.argv[3]) except IndexError: targetport = port except (ValueError, IndexError): print 'Usage: %s port targethost [targetport]' % sys.argv[0] sys.exit(1) #sys.stdout = open('forwaring.log', 'w') Forwarding(port, targethost, targetport).start()
相關(guān)文章
Python快速生成隨機(jī)密碼超簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Python快速生成隨機(jī)密碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08在Python的Flask框架中構(gòu)建Web表單的教程
Flask框架中自帶一個(gè)Form表單類,通過(guò)它的子類來(lái)實(shí)現(xiàn)表單將相當(dāng)愜意,這里就為大家?guī)?lái)Python的Flask框架中構(gòu)建Web表單的教程,需要的朋友可以參考下2016-06-06Python實(shí)現(xiàn)創(chuàng)建模塊的方法詳解
導(dǎo)入一個(gè)模塊,我們一般都會(huì)使用?import?關(guān)鍵字,但有些場(chǎng)景下?import?難以滿足我們的需要。所以除了?import?之外還有很多其它導(dǎo)入模塊的方式,下面就來(lái)介紹一下2022-07-07Python將多個(gè)圖像合并輸出的實(shí)現(xiàn)方法
這篇文章主要介紹了Python將多個(gè)圖像合并輸出的實(shí)現(xiàn)方法,本文介紹了兩種將多個(gè)圖像合并為一個(gè)輸出的方法:使用PIL庫(kù)或使用OpenCV和NumPy,這些庫(kù)都可以使用Python中的簡(jiǎn)單語(yǔ)法和少量的代碼來(lái)完成此任務(wù),需要的朋友可以參考下2023-06-06python json.loads兼容單引號(hào)數(shù)據(jù)的方法
今天小編就為大家分享一篇python json.loads兼容單引號(hào)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Python實(shí)現(xiàn)讀取TXT文件數(shù)據(jù)并存進(jìn)內(nèi)置數(shù)據(jù)庫(kù)SQLite3的方法
這篇文章主要介紹了Python實(shí)現(xiàn)讀取TXT文件數(shù)據(jù)并存進(jìn)內(nèi)置數(shù)據(jù)庫(kù)SQLite3的方法,涉及Python針對(duì)txt文件的讀取及sqlite3數(shù)據(jù)庫(kù)的創(chuàng)建、插入、查詢等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Pandas?Matplotlib保存圖形時(shí)坐標(biāo)軸標(biāo)簽太長(zhǎng)導(dǎo)致顯示不全問(wèn)題的解決
在使用matplotlib作圖的時(shí)候,有的時(shí)候會(huì)遇到畫圖時(shí)顯示不全和圖片保存時(shí)不完整的問(wèn)題,這篇文章主要給大家介紹了關(guān)于Pandas?Matplotlib保存圖形時(shí)坐標(biāo)軸標(biāo)簽太長(zhǎng)導(dǎo)致顯示不全問(wèn)題的解決方法,需要的朋友可以參考下2022-06-06