简体   繁体   中英

Finding ports opened for a given PID programmatically

In Linux, given a PID, is there a way to find the list of ports it opened programmatically (preferably Python)? Utilities such as lsof/netstat not available.

Something like this? Note: needs to run w/ sudo-rights , otherwise you have no access to the PIDs .

import psutil
tmp = psutil.net_connections()
for i in tmp:
    if i.pid==30264:
        print( i.laddr[1] )

PS: You weren't clear on whether you meant local or remote ports; adjust laddr to raddr accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM