Search code examples
pythonmultithreadingsocketsmultiprocessingraw-sockets

Connect and handle multiple raw sockets at once in Python 2.x


I am new to Python, and trying to learn it "on the job". And I am required to do this.

I am required to communicate with 3 servers with a raw socket connection. I can easily do that in a sequential manner. But I was wondering if there is a way I can communicate with these 3 servers at once? All 3 servers have different IP addresses.

Basically try to do the following but in 1 step:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST1, PORT1))
s.connect((HOST2, PORT1))
s.connect((HOST3, PORT1))

which is also need later s.sendall() & s.recv() to be parallelized.


Solution

  • This answer using threading actually worked out for me.