国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現端口轉發器的方法

2020-02-23 00:21:30
字體:
來源:轉載
供稿:網友

本文實例講述了python實現端口轉發器的方法。分享給大家供大家參考。具體如下:

下面的python代碼實現端口轉發器,支持udp端口轉發

由于工作需要用到一個端口轉發器,并且要求支持TCP和UDP協議。在網上找了蠻久,但沒有中意的。于是就自己寫了一個。這個轉發器是基于python cookbook的一個示例改寫的,原先的這個示例只支持TCP協議,我這里增加了UDP協議的支持,程序寫的不怎么好,不過它確實能用!

portmap.py代碼如下:
代碼如下:#-* -coding: UTF-8 -* -
'''
Created on 2012-5-8
@author: qh
'''
import time,socket,threading
def log(strLog):
    strs=time.strftime("%Y-%m-%d %H:%M:%S")
    print strs+"->"+strLog
class pipethread(threading.Thread):
    '''
    classdocs
    '''
    def __init__(self,source,sink):
        '''
        Constructor
        '''
        threading.Thread.__init__(self)
        self.source=source
        self.sink=sink
        log("New Pipe create:%s->%s" % (self.source.getpeername(),self.sink.getpeername()))
    def run(self):
        while True:
            try:
                data=self.source.recv(1024)
                if not data: break
                self.sink.send(data)
            except Exception ,ex:
                log("redirect error:"+str(ex))
                break
        self.source.close()
        self.sink.close()
class portmap(threading.Thread):
    def __init__(self,port,newhost,newport,local_ip=''):
        threading.Thread.__init__(self)
        self.newhost=newhost
        self.newport=newport
        self.port=port
        self.local_ip=local_ip
        self.sock=None

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彰武县| 莱阳市| 封开县| 安福县| 濮阳市| 徐汇区| 珲春市| 会东县| 朝阳县| 邵武市| 胶州市| 青铜峡市| 普陀区| 容城县| 通城县| 金门县| 阿坝县| 内江市| 七台河市| 曲阳县| 天长市| 巴中市| 昆山市| 都兰县| 长海县| 韶山市| 荆门市| 海口市| 葵青区| 长阳| 大宁县| 壶关县| 九龙县| 沈阳市| 泽州县| 洪雅县| 高要市| 邓州市| 武威市| 丰镇市| 巢湖市|