Simply create a temporary socket and then try to bind to the port to see if it's available. Close the socket after validating that the port is available.
def tryPort(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = False
try:
sock.bind(("0.0.0.0", port))
result = True
except:
print("Port is in use")
sock.close()
return result
from : https://stackoverflow.com/questions/43270868/verify-if-a-local-port-is-available-in-python
No comments:
Post a Comment