Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain ports that a process in listening on?

Tags:

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

like image 966
Dominique Avatar asked Jun 07 '10 05:06

Dominique


People also ask

How can you find out which process is listening on a TCP?

There are many ways to find out which Windows process is listening on a TCP or UDP port. You can either use Resource Monitor or the TCPView GUI applications. Other than that, command line tools can also be utilized for specified purpose, such as Windows PowerShell and Command Prompt.


2 Answers

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')] >>> 
like image 50
Giampaolo Rodolà Avatar answered Sep 21 '22 00:09

Giampaolo Rodolà


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!

like image 44
BillyBBone Avatar answered Sep 19 '22 00:09

BillyBBone