I'm trying to implement a tcp proxy with python,
So I need to connect two sockets directly, passing input and output in between.
In golang, I simply do a io.Copy
, what's the equivalent in Python 2.6?
go func() {
defer conn1.Close()
defer conn2.Close()
io.Copy(conn1, conn2)
}()
You may use function like this:
def CopyProxy(conn1, conn2):
while True:
data = conn2.recv(BUFFER_SIZE)
try:
conn1.send(data)
if not data:
conn1.close()
conn2.close()
break
except Exception:
break
Then launch them in separate threads:
# conn1 and conn2 - previously opened connections "to" and "from"
t1 = threading.Thread(target=CopyProxy, args=[conn1, conn2])
t2 = threading.Thread(target=CopyProxy, args=[conn2, conn1])
t1.start()
t2.start()