如何使用python读取应用程序的输出 [英] How to read output from an application using python

查看:48
本文介绍了如何使用python读取应用程序的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在尝试记录传出 TCP 连接列表,然后将当前列表与之前的列表进行比较,以查看已建立的连接.但是,TCPView.exe 似乎不走运.我可以用这个运行应用程序:

Currently trying to record a list of outgoing TCP connections and then compare the present list to the previous list in order to see what connections have been established. However, TCPView.exe seems to be of no luck. I am able to run the application with this:

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 信息

However, this is not returning any text. Not for sure where to go with this as I have no idea how to read the TCP information in python

推荐答案

应该是这样的

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) 

这篇关于如何使用python读取应用程序的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆