def get_port_stats(port):
 cnts = defaultdict(int)
 for c in psutil.net_connections():
  c_port = c.laddr[1]
  if (c_port != port):
   continue
  status = c.status.lower()
  cnts[status] += 1
 return cnts

def get_port_stats ( port ) : cnts = defaultdict ( int ) for c in psutil . net_connections ( ) : c_port = c . laddr [ 1 ] if ( c_port != port ) : continue status = c . status . lower ( ) cnts [ status ] += 1 return cnts
iterate over connections and count states for specified port .