def read_pid_file(filename):
    """
    Reads a pid file and returns the contents. Pid files have 1 or 2 lines; the first line is always the pid, and the
    optional second line is the port the server is listening on.

    :param filename: Filename to read
    :return: the tuple (pid, port) with the pid in the file and the port number if it exists. If the port number doesn't
        exist, then port is None.
    """
    try:
        pid, port = open(filename, 'r').readlines()
        pid, port = int(pid), int(port)
    except ValueError:
        try:
            pid, port = int(open(filename, 'r').read()), None
        except ValueError:
            pid, port = None, None
    return pid, port
