Joshua Bakker Joshua Bakker - 1 month ago 7
Python Question

Python socket - connected socket

I'm currently coding a socket server in Python (VS2012 addon)
But I'm stuck and can't get a new socket from an accepted connection.

while (True):
new = sock.accept()

acc_addr = new[1]
ip = acc_addr[0]
connid = acc_addr[1]

print 'Received connection from ' + ip + ':' + connid.__str__()


This is what I use, but now I want a new socket() instance for the connected user for my other class (for receiving, sending etc), but how would I get the new socket() instance?

Answer

Your new tuple returned by accept() has 2 elements: at [1], there is the address tuple, consisting of the ip and the port (named connid by you) - and at 0 there is a socket object which can be used for communicating with the client.

So just do clsock = new[0] and you have your socket() instance.