Get random available port with Python

Find a random available local port with this Python snippet.

#!/usr/bin/env python
from socket import socket

with socket() as s:

From shell, this returns on STDOUT a free port number. There is a slight race condition where between end of this program and start of your shell program, another program could grab this port.