如何使用python读取应用程序的输出

2024-09-22 18:16:32 发布

您现在位置:Python中文网/ 问答频道 /正文

当前正在尝试记录传出TCP连接的列表,然后将当前列表与上一个列表进行比较,以查看已建立的连接。然而,TCPView.exe似乎运气不佳。我可以通过以下方式运行应用程序:

def get_tcp_conns():
    process = Popen([r"C:\Users\PC\Downloads\TCPView\Tcpview.exe"], stdout=PIPE, shell=True)
    for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"):
        print(line)  # Do whatever here

但是,这不会返回任何文本。我不知道该怎么做,因为我不知道如何在python中读取TCP信息


Tags: 应用程序列表getdefstdout方式记录line
1条回答
网友
1楼 · 发布于 2024-09-22 18:16:32

应该是

import subprocess 

def get_tcp_conns():
    process = subprocess.run("C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe './C:Users/PC/Downloads/TCPView/Tcpview.exe' ", shell=True, capture_output=True)
    print(process.stdout) 

相关问题 更多 >