1 Star 0 Fork 0

江旭川/笔记

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
获取进程信息.py 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
江旭川 提交于 2021-12-14 06:29 . 1
'''
1、通过程序,扫描被进程占用的端口(TCP及UDP)和进程相关信息,并输出。
2、输出结果包括:进程id(pid)、进程名、占用端口、端口类型、Local IP、进程地址。
'''
import psutil
def get_process_info():
# 获取当前的网络连接信息
net_con = psutil.net_connections()
port_type =''
for con_info in net_con:
pid = con_info[6]
ip = con_info[3].ip
port = con_info[3].port
if con_info[2] == 1:
port_type = 'TCP'
elif con_info[2] == 2:
port_type = 'UDP'
# 进程对象
p = psutil.Process(con_info[6])
# 进程名
process_name = p.name()
process_addr = p.open_files()
print('pid: %-8s 进程名: %-20s 占用端口: %-8s 端口类型: %-8s Local IP: %-20s 进程地址: %s' %
(pid,process_name,port,port_type,ip,process_addr))
if __name__ == "__main__":
get_process_info()
# net_con = psutil.net_connections()
# for con_info in net_con:
# print(con_info)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jiang-xuchuan/note.git
git@gitee.com:jiang-xuchuan/note.git
jiang-xuchuan
note
笔记
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385