I have a project where I spawn subprocesses, which again take a port number to bind to. The port number is assigned by my Python script, where I simply take a random port between 49152 and 65535.
I want to verify if the port is available and not used by any other tool on the local system (*nix).
From another question I have this snippet:
import socket;
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1',80))
if result == 0:
print "Port is open"
else:
print "Port is not open"
Could I use this in my case? Would this not open a port and not close it for further use?