2012-10-09 4 views
1
내가

파이썬에서 동적 포트 포워딩을 만드는 방법은 무엇입니까?

ser = forwarder('127.0.0.1', 7774, 'google.com.ua', 80) 
evLoop = AsyncEventLoop() 

나는 즉시 remoteip, remoteport 변경할 수 코드에서 시작 코드 (found here) 조금 (추가 클래스 AsyncEventLoop를)

import socket,asyncore 
import threading 

class forwarder(asyncore.dispatcher): 
    def __init__(self, ip, port, remoteip,remoteport,backlog=5): 
     asyncore.dispatcher.__init__(self) 
     self.remoteip=remoteip 
     self.remoteport=remoteport 
     self.create_socket(socket.AF_INET,socket.SOCK_STREAM) 
     self.set_reuse_addr() 
     self.bind((ip,port)) 
     self.listen(backlog) 

    def handle_accept(self): 
     conn, addr = self.accept() 
     # print '--- Connect --- ' 
     sender(receiver(conn),self.remoteip,self.remoteport) 
    def closef(self): 
     self.close() 

class receiver(asyncore.dispatcher): 
    def __init__(self,conn): 
     asyncore.dispatcher.__init__(self,conn) 
     self.from_remote_buffer='' 
     self.to_remote_buffer='' 
     self.sender=None 

    def handle_connect(self): 
     pass 

    def handle_read(self): 
     read = self.recv(4096) 
     # print '%04i -->'%len(read) 
     self.from_remote_buffer += read 

    def writable(self): 
     return (len(self.to_remote_buffer) > 0) 

    def handle_write(self): 
     sent = self.send(self.to_remote_buffer) 
     # print '%04i <--'%sent 
     self.to_remote_buffer = self.to_remote_buffer[sent:] 

    def handle_close(self): 
     self.close() 
     if self.sender: 
      self.sender.close() 

class sender(asyncore.dispatcher): 
    def __init__(self, receiver, remoteaddr,remoteport): 
     asyncore.dispatcher.__init__(self) 
     self.receiver=receiver 
     receiver.sender=self 
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM) 
     self.connect((remoteaddr, remoteport)) 

    def handle_connect(self): 
     pass 

    def handle_read(self): 
     read = self.recv(4096) 
     # print '<-- %04i'%len(read) 
     self.receiver.to_remote_buffer += read 

    def writable(self): 
     return (len(self.receiver.from_remote_buffer) > 0) 

    def handle_write(self): 
     sent = self.send(self.receiver.from_remote_buffer) 
     # print '--> %04i'%sent 
     self.receiver.from_remote_buffer = self.receiver.from_remote_buffer[sent:] 

    def handle_close(self): 
     self.close() 
     self.receiver.close() 

class AsyncEventLoop(threading.Thread): 
    def run(self): 
     asyncore.loop() 

수정

? 파리가 변경 불가능한 경우,이 스레드가 새로운 설정 (remoteip, remoteport)으로 시작하는 것을 중지 하시겠습니까?

답변

0

예, 할 수 있습니다

ser.remoteip, ser.remoteport = IP, port