parameters: (iftop_output, duration=40)
return_statement: return result
docstring: parse output of iftop to map which key is ip:port value is map of in/out statistic
docstring_summary: parse output of iftop to map which key is ip:port value is map of in/out statistic
docstring_tokens: parse output of iftop to map which key is ip : port value is map of in / out statistic
function: def parse_iftop(iftop_output, duration=40):
      """ parse output of iftop to map which key is ip:port value is map of in/out statistic """
      result = collections.defaultdict(lambda : {"in": 0, "out": 0})
      raw = [line.strip() for line in iftop_output.splitlines()]
      data = []
  
      # only interested in part two divided by ----
      part = 0
      for line in raw:
          if "------------" in line:
              part += 1
              if part == 2:
                  break
          else:
              if part == 1:
                  data.append(line)
  
      for line_no in range(0, len(data), 2):
          line1 = data[line_no].split()
          line2 = data[line_no + 1].split()
          src = line1[1]
          dst = line2[0]
  
          if duration == 2:
              col_index = -4
          elif duration == 10:
              col_index = -3
          elif duration == 40:
              col_index = -2
          else:
              col_index = -2
  
          out_byte = convert_to_byte(line1[col_index])
          in_byte = convert_to_byte(line2[col_index])
  
          result[src]["in"] += in_byte
          result[src]["out"] += out_byte
  
          result[dst]["in"] += out_byte
          result[dst]["out"] += in_byte
  
      return result
function_tokens: def parse_iftop ( iftop_output , duration = 40 ) : result = collections . defaultdict ( lambda : { "in" : 0 , "out" : 0 } ) raw = [ line . strip ( ) for line in iftop_output . splitlines ( ) ] data = [ ] # only interested in part two divided by ---- part = 0 for line in raw : if "------------" in line : part += 1 if part == 2 : break else : if part == 1 : data . append ( line ) for line_no in range ( 0 , len ( data ) , 2 ) : line1 = data [ line_no ] . split ( ) line2 = data [ line_no + 1 ] . split ( ) src = line1 [ 1 ] dst = line2 [ 0 ] if duration == 2 : col_index = - 4 elif duration == 10 : col_index = - 3 elif duration == 40 : col_index = - 2 else : col_index = - 2 out_byte = convert_to_byte ( line1 [ col_index ] ) in_byte = convert_to_byte ( line2 [ col_index ] ) result [ src ] [ "in" ] += in_byte result [ src ] [ "out" ] += out_byte result [ dst ] [ "in" ] += out_byte result [ dst ] [ "out" ] += in_byte return result