find_unbound_port returns a (pseudo-)random unbound port on localhost.

import random
import socket

# range of ports where available ports can be found
PORT_RANGE = [33000,60000]

def find_unbound_port():
    """
    Returns an unbound port number on 127.0.0.1.
    """
    port = random.randint(*PORT_RANGE)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.bind(("127.0.0.1", port))
    except socket.error:
        port = get_port()
    return port

if __name__ == "__main__":
    print find_unbound_port()