【发布时间】:2020-10-20 14:41:02
【问题描述】:
我正在编写一个脚本来使用 subprocess.Popen 获取 netstat 状态。
cmd = 'netstat -nlpt | grep "java" | grep -v tcp6'
result1 = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True )
stdout, stderr=result1.communicate()
for line in iter(result1.stdout):
print(line)
上面给出了 ValueError: I/O operation on closed file.。有什么办法可以得到直播输出。在live output from subprocess command 他们使用 writen 和 readlines 在这里我只需要打印实时状态请有人帮助我解决这个问题。谢谢!
【问题讨论】:
标签: python python-3.x shell subprocess netstat