How do I get the ports that a process is listening on using python? The pid of the process is known.
If you don't want to parse the output of a program like netstat or lsof, you can grovel through the /proc filesystem and try to find documentation on the files within.  /proc/<pid>/net/tcp might be especially interesting to you.  Of course, the format of those files might change between kernel releases, so parsing command output is generally considered more reliable.
How to obtain ports that a process in listening on?