def listening_ports():
ports = []<EOL>if not os.path.exists(PROC_TCP):<EOL><INDENT>return ports<EOL><DEDENT>with open(PROC_TCP) as fh:<EOL><INDENT>for line in fh:<EOL><INDENT>if '<STR_LIT>' not in line:<EOL><INDENT>continue<EOL><DEDENT>parts = line.lstrip('<STR_LIT:U+0020>').split('<STR_LIT:U+0020>')<EOL>if parts[<NUM_LIT:2>] != '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>local_port = parts[<NUM_LIT:1>].split('<STR_LIT::>')[<NUM_LIT:1>]<EOL>local_port = int('<STR_LIT>' + local_port, base=<NUM_LIT:16>)<EOL>ports.append(local_port)<EOL><DEDENT><DEDENT>return ports<EOL>
Reads listening ports from /proc/net/tcp