创建Procss类,3506是pid
p = psutil.Process(3506)
实例p里的内容应该不是固定的,实际进程消失的情况下调用实例内的函数会抛出psutil.NoSuchProcess错误(至少0.6版的process_iter()返回的Procss实例会如此)
获取当前连接
>>> a = p.connections()
>>> print type(a[0])
<class 'psutil._common.pconn'>
>>> print a
[pconn(fd=5, family=2, type=1, laddr=('172.20.0.93', 2677), raddr=(), status='LISTEN')]
获取打开的文件
>>> print p.open_files()
[popenfile(path='/var/log/middleware/info.log', fd=1), popenfile(path='/var/log/middleware/errlog.log', fd=2), popenfile(path='/var/log/middleware/info.log', fd=3), popenfile(path='/var/log/middleware/errlog.log', fd=4)]
上述方法可以解决我原来在论坛里没人回答的问题
点击(此处)折叠或打开