title: How can I programatically forward TCP connections in Python?
body: <p>I would like to programmatically redirect incoming TCP connections on the local machine to other machines and conditionally change them to other machines </p>
  
  <p>For example I'd like to forward incoming connections on port <code>9001</code> to <code>www.google.com:80</code> then after sometime I would like to close 9001 on the local machine and instead start forwarding incoming connections on <code>9400</code> to <code>www.yahoo.com:80</code> or potentially have both forwarding rules in operation at the same time. </p>
  
  <p>I initially thought I could do this with iptables but I'm not sure. I've also looked at TCP socket proxies, but I'm not interested in the content of the connections just being able to redirect and close them programatically.  </p>