title: How to make a dynamic port forwarding on python?
body: <p>I modified the code (<a href="http://code.activestate.com/recipes/483730-port-forwarding/" rel="nofollow">found here</a>) a bit (added class AsyncEventLoop)</p>
  
  <pre><code>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 --&gt;'%len(read)
          self.from_remote_buffer += read
  
      def writable(self):
          return (len(self.to_remote_buffer) &gt; 0)
  
      def handle_write(self):
          sent = self.send(self.to_remote_buffer)
          # print '%04i &lt;--'%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 '&lt;-- %04i'%len(read)
          self.receiver.to_remote_buffer += read
  
      def writable(self):
          return (len(self.receiver.from_remote_buffer) &gt; 0)
  
      def handle_write(self):
          sent = self.send(self.receiver.from_remote_buffer)
          # print '--&gt; %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()
  </code></pre>
  
  <p>starting in the code</p>
  
  <pre><code>ser = forwarder('127.0.0.1', 7774, 'google.com.ua', 80)
  evLoop = AsyncEventLoop()
  </code></pre>
  
  <p>Can I change <code>remoteip, remoteport</code> on the fly? If the fly is not possible to change, as well stop this thread to start with the new settings (remoteip, remoteport)?</p>