我试图找出我的服务器(应该在127.0.0.1:5000
上运行)是否正在实际运行。我试图用psutil.net_connections()
来解决这个问题:
filter(lambda conn: conn.raddr.ip == '127.0.0.1' and conn.raddr.port == 5000, psutil.net_connections())
这应该给我与我的服务器相对应的项目,为了检查我是否真的得到了一些东西,我只需检查len(tuple(...)))
。但是,使用tuple(...)
会给我AttributeError: 'tuple' object has no attribute 'ip'
,我没有得到,因为内部元组(即conn.raddr
确实有一个“ip”属性)
定期循环时也会发生这种情况:
In [22]: for conn in psutil.net_connections():
...: if conn.raddr.ip == '127.0.0.1' and conn.raddr.port == 5000:
...: break
...: else:
...: print('server is down')
但是像这样使用它时,它会工作
In [23]: a=psutil.net_connections()[0]
In [24]: a.raddr.ip
Out[24]: '35.190.242.205'
psutil版本:5.7.2
并非所有的
raddr
都有ip
属性。文件说:因此,在尝试访问
ip
和port
属性之前,应该检查raddr
是否为空相关问题 更多 >
编程相关推荐