

@wrap_exceptions
def get_connections(self, kind='inet'):
    'Return connections opened by process as a list of namedtuples.\n        The kind parameter filters for connections that fit the following\n        criteria:\n\n        Kind Value      Number of connections using\n        inet            IPv4 and IPv6\n        inet4           IPv4\n        inet6           IPv6\n        tcp             TCP\n        tcp4            TCP over IPv4\n        tcp6            TCP over IPv6\n        udp             UDP\n        udp4            UDP over IPv4\n        udp6            UDP over IPv6\n        all             the sum of all the possible families and protocols\n        '
    inodes = {
        
    }
    for fd in os.listdir(('/proc/%s/fd' % self.pid)):
        try:
            inode = os.readlink(('/proc/%s/fd/%s' % (self.pid, fd)))
        except OSError:
            continue
        if inode.startswith('socket:['):
            inode = inode[8:][:(- 1)]
            inodes[inode] = fd
    if (not inodes):
        return []

    def process(file, family, type_):
        retlist = []
        try:
            f = open(file, 'r')
        except IOError:
            err = sys.exc_info()[1]
            if ((err.errno == errno.ENOENT) and file.endswith('6')):
                return []
            else:
                raise
        try:
            f.readline()
            for line in f:
                if (family in (socket.AF_INET, socket.AF_INET6)):
                    (_, laddr, raddr, status, _, _, _, _, _, inode) = line.split()[:10]
                    if (inode in inodes):
                        laddr = self._decode_address(laddr, family)
                        raddr = self._decode_address(raddr, family)
                        if (type_ == socket.SOCK_STREAM):
                            status = _TCP_STATES_TABLE[status]
                        else:
                            status = ''
                        fd = int(inodes[inode])
                        conn = nt_connection(fd, family, type_, laddr, raddr, status)
                        retlist.append(conn)
                elif (family == socket.AF_UNIX):
                    tokens = line.split()
                    (_, _, _, _, type_, _, inode) = tokens[0:7]
                    if (inode in inodes):
                        if (len(tokens) == 8):
                            path = tokens[(- 1)]
                        else:
                            path = ''
                        fd = int(inodes[inode])
                        type_ = int(type_)
                        conn = nt_connection(fd, family, type_, path, None, '')
                        retlist.append(conn)
                else:
                    raise ValueError(family)
            return retlist
        finally:
            f.close()
    tcp4 = ('tcp', socket.AF_INET, socket.SOCK_STREAM)
    tcp6 = ('tcp6', socket.AF_INET6, socket.SOCK_STREAM)
    udp4 = ('udp', socket.AF_INET, socket.SOCK_DGRAM)
    udp6 = ('udp6', socket.AF_INET6, socket.SOCK_DGRAM)
    unix = ('unix', socket.AF_UNIX, None)
    tmap = {
        'all': (tcp4, tcp6, udp4, udp6, unix),
        'tcp': (tcp4, tcp6),
        'tcp4': (tcp4,),
        'tcp6': (tcp6,),
        'udp': (udp4, udp6),
        'udp4': (udp4,),
        'udp6': (udp6,),
        'unix': (unix,),
        'inet': (tcp4, tcp6, udp4, udp6),
        'inet4': (tcp4, udp4),
        'inet6': (tcp6, udp6),
    }
    if (kind not in tmap):
        raise ValueError(('invalid %r kind argument; choose between %s' % (kind, ', '.join([repr(x) for x in tmap]))))
    ret = []
    for (f, family, type_) in tmap[kind]:
        ret += process(('/proc/net/%s' % f), family, type_)
    os.stat(('/proc/%s' % self.pid))
    return ret

dataset/ETHPy150Open packages/psutil/psutil/_pslinux.py Process.get_connections/original