import socket


def random_port(bind="127.0.0.1"):
    """Returns a random port which is not in use"""
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.bind((bind, 0))
        _, port = sock.getsockname()
        return port
    finally:
        sock.close()