Find ports a program uses with python
Question: I want to find the ports used by 'plugin-container.exe' so I can monitor what
IP addresses interact with that program, The problem is there are two 'plugin-
container.exe's. I use Firefox Developer Edition.

I already have the monitoring part down but I need to automate getting the
ports some how. From what I've seen, getting them means knowing what PID the
process is using, two processes = 2 PIDs. ;_;

I could add BOTH of them but there is a ton of traffic going though my browser
and it kills my program when I put all 4 ports in manually.

Right now I'm using this to get the list, re.findall to filter the 'plugin-
container.exe' in the list, I then use psutil to find what ports.

I feel like there is an easier way to do all of this.

> import os, sys, win32api, re, psutil
>
> tasklistrl = os.popen("tasklist").readlines()
>
> tasklistr = os.popen("tasklist").read()


Answer: If you want a nice way to find processes by executable name using psutil, then
you should use
[process_iter](https://pythonhosted.org/psutil/#psutil.process_iter) and
[cmdline](https://pythonhosted.org/psutil/#psutil.Process.cmdline):

    
    
    my_processes = [x for x in psutil.process_iter() if os.path.split(x.cmdline()[0])[1] == 'python']
    

(substitute `'python'` with executable name you want)

