首页 > 解决方案 > 如何使用 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 信息

标签: pythontcp

解决方案


应该是这样的

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) 

推荐阅读