def port_int(p):
    """Pass through a port number (as a number or a string) provided it is valid and in range, otherwise raise an exception"""
    try:
        port=int(p)
    except:
        raise ValueError("Invalid port number")

    if port>=0 and port<=65535:
        return port
    else:
        raise ValueError("Port number out of range")