def get_fd_info():
    '''
    Gathers and correlates fd info from 3 sources:

    1. gc over all objects that have a fileno(); this is most 'socket-like' things
    2. infra Context object data structures
    3. psutil information
    4. /proc filesystem (if available)

    This function is a little bit open-ended, we can probably continue
    to find additional sources of information
    '''
    import psutil

    fd_info = collections.defaultdict(
        lambda: {'gc_objs': [], '/proc': {}, 'context': [], 'psutil': {}})

    # 1 - all objects with filenos
    for obj in gc.get_objects():
        try:
            fd = None
            if hasattr(obj, 'fileno') and callable(obj.fileno):
                fd = obj.fileno()
        except:  # there are a zillion boring reasons this may fail
            pass
        if isinstance(fd, (int, long)):
            fd_info[fd]['gc_objs'].append(obj)

    # 2 - inspection of data-structures in context
    ctx = context.get_context()
    socks = []
    if ctx.connection_mgr:
        for model in ctx.connection_mgr.server_models.values():
            socks.extend(model.active_connections)
    if ctx.client_sockets:
        socks.extend(ctx.client_sockets)
    if ctx.server_group:
        socks.extend(ctx.server_group.socks.values())
    for sock in socks:
        fd_info[sock.fileno()]['context'].append(sock)

    # 3 - psutil information
    process = psutil.Process()
    for f in process.get_open_files():
        if f.fd == -1:
            continue
        fd_info[f.fd]['psutil']['path'] = f.path
    for conn in process.get_connections(kind='all'):
        if conn.fd == -1:
            continue
        fd_info[conn.fd]['psutil'].update(vars(conn))
        del fd_info[conn.fd]['psutil']['fd']

    # 4 - /proc filesystem
    proc_path = "/proc/" + str(os.getpid())
    if not os.path.exists(proc_path):
        return dict(fd_info)

    for fd in os.listdir(proc_path + "/fd"):
        try:
            inode = os.readlink(proc_path + "/fd/" + fd)
        except __HOLE__:
            continue
        else:
            fd = int(fd)
            fd_info[fd]['/proc']['inode'] = inode

    return dict(fd_info)
dataset/ETHPy150Open paypal/support/support/meta_service/meta_service.py/get_fd_info