代码拉取完成,页面将自动刷新
'''
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。