import socket


def find_free_ports(how_many=1):
    """Return a list of n free port numbers on localhost"""
    results = []
    sockets = []
    for x in range(how_many):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(('localhost', 0))
        # work out what the actual port number it's bound to is
        addr, port = s.getsockname()
        results.append(port)
        sockets.append(s)

    for s in sockets:
        s.close()

    return results