views:

72

answers:

3

How do I get the ports that a process is listening on using python? The pid of the process is known.

+1  A: 

You can use netstat -lnp, last column will contain pid and process name. In Python you can parse output of this command.

Michał Niklas
+2  A: 

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 the command output parsing approach is generally considered more reliable.

Forest
+2  A: 

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, PIPE
p1 = Popen(['lsof', '-a', '-p23819', '-i4'], stdout=PIPE)
p2 = Popen(["grep", "LISTEN"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]

Hope this helps!

BillyBBone