I am using the below python code to get two available unbind-ed port. I am running another c++ program, which binds to port 8000 and waits for a message to come, before running this program. Still it gives port 8000 as unbind-ed. I am sure the port 800 is listening.
The code is not catching any exceptions.
Any help please.
def get_next_port():
global udp_port
global result
global port_list
global number
try:
print "try"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', udp_port))
port_list.append(udp_port)
print udp_port
udp_port = udp_port+1
number = number+1
sock.close()
except socket.error as msg:
print "except"
if msg.errno == errno.ECONNREFUSED:
print "already binded"
while(number < 2):
get_next_port()