The read_proc_connections() function, defined on line 58, starts by creating an empty list, conn_details, which will store the details of each PID connection:
058 def read_proc_connections(proc):
059 """
060 Read connection properties from a process.
061 :param proc: An object representing a running process.
062 :return conn_details: A list of process connection
063 properties.
064 """
065 conn_details = []
For each connection in the provided process, we create a conn_items dictionary, and store within it, the details of each connection, including the status of the connection and the local and remote IP addresses and ports. As seen before, we use the getattr() method, querying for named attributes of the specified object and storing the returned value in our dictionary. If the named object does not exist, we use None or empty strings as default values defined as the third input of the getattr() function.
We then append the dictionary of details for each connection to the conn_details list which, after this process has completed for each connection, is itself returned to the calling function:
066 for conn in proc.connections():
067 conn_items = {}
068 conn_items['fd'] = getattr(conn, 'fd', None)
069 conn_items['status'] = getattr(conn, 'status', None)
070 conn_items['local_addr'] = "{}:{}".format(
071 getattr(conn.laddr, 'ip', ""), getattr(
072 conn.laddr, 'port', ""))
073 conn_items['remote_addr'] = "{}:{}".format(
074 getattr(conn.raddr, 'ip', ""), getattr(
075 conn.raddr, 'port', ""))
076
077 conn_details.append(conn_items)
078 return conn_details