How to obtain ports that a process in listening on? How to obtain ports that a process in listening on? python python

How to obtain ports that a process in listening on?


You can use psutil:

>>> import psutil>>> p = psutil.Process(2549)>>> p.name()'proftpd: (accepting connections)'>>> p.connections()[connection(fd=1, family=10, type=1, local_address=('::', 21), remote_address=(), status='LISTEN')]

...To filter for listening sockets:

>>> [x for x in p.get_connections() if x.status == psutil.CONN_LISTEN][connection(fd=1, family=10, type=1, local_address=('::', 21), remote_address=(), status='LISTEN')]>>>


There are two parts to my answer:

1. Getting the information in the shell

For the first part, netstat would work, but I prefer using lsof, since it can be used to extract a more informative and concise list. The exact options to use may vary based on your OS, kernel and compilation options, but I believe you want something like this:

lsof -a -p23819 -i4

Where 23819 is the PID you are selecting for, and i4 denotes all IPv4 sockets (though you might want i6 for IPv6, as the case may be). From there, you can pipe through grep to select only listening sockets.

lsof -a -p23819 -i4 | grep LISTEN

(In lsof version 4.82, you can additionally use the -sTCP:LISTEN flag instead of grep to select listening sockets, though this option doesn't seem to be available back in version 4.78)

2. Calling lsof from Python

You should be able to call lsof and read the output, from Python, using the subprocess module, like so:

from subprocess import Popen, PIPEp1 = Popen(['lsof', '-a', '-p23819', '-i4'], stdout=PIPE)p2 = Popen(["grep", "LISTEN"], stdin=p1.stdout, stdout=PIPE)output = p2.communicate()[0]

Hope this helps!


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.