def ports_for_pids(pids):
        netstat = Popen(["netstat","-lpn"], stdout=PIPE,
            stderr=PIPE).communicate()[0].split("\n")
        addr_pos = None
        pid_pos = None
        addr_heading = "Local Address"
        pid_heading = "PID/Program name"
        for row in netstat:
            if addr_heading in row and pid_heading in row:
                addr_pos = row.index(addr_heading)
                pid_pos = row.index(pid_heading)
            if addr_pos is not None:
                pid = row[pid_pos:].split("/")[0]
                if pid in pids:
                    port = row[addr_pos:].split(" ", 1)[0].split(":")[1]
                    yield (pid, port)
dataset/ETHPy150Open stephenmcd/gunicorn-console/gunicorn_console.py/ports_for_pids/original