控制Windows控制台应用程序w / stdin管道 [英] Controlling a Windows Console App w/ stdin pipe

查看:499
本文介绍了控制Windows控制台应用程序w / stdin管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用subprocess模块​​从Python控制控制台应用程序(来自Segger的JTAG应用程序)。应用程序对于stdout行为正确,但stdin似乎没有被读取。如果启用shell,我可以键入到输入和控制应用程序,但我需要以编程方式。同样的代码适用于向cmd.exe等命令发出命令。

I am trying to control a console application (JTAG app from Segger) from Python using the subprocess module. The application behaves correctly for stdout, but stdin doesn't seem to be read. If enable the shell, I can type into the input and control the application, but I need to do this programmatically. The same code works fine for issuing commands to something like cmd.exe.

我猜想键盘正在直接读取,而不是stdin。任何想法如何可以发送应用程序输入?

I'm guessing that the keyboard is being read directly instead of stdin. Any ideas how I can send the application input?

from subprocess import Popen, PIPE, STDOUT
jtag = Popen('"C:/Program Files/SEGGER/JLinkARM_V402e/JLink.exe"', shell=True,
                        universal_newlines=True,
                        stdin=PIPE,
                        stdout=PIPE,
                        stderr=STDOUT)

jtag.stdin.write('usb\n')
jtag.stdin.flush()

print "Stdout:"
while True:
    s = jtag.stdout.readline()
    if not s:
        break
    print s,

jtag.terminate()


推荐答案

应用程序真正在寻找键盘输入。如果是,您可以尝试Win32消息传递,或通过自动发送键盘输入。

As shoosh says, I'd try to verify that the application really is looking for keyboard input. If it is, you can try Win32 message passing, or sending it keyboard input via automation.

对于消息传递路由,您可以使用 EnumWindows 函数通过ctypes找到你之前的窗口,然后使用PostMessage发送它WM_KEYDOWN

For the message passing route, you could use the EnumWindows function via ctypes to find the window you're after, then using PostMessage to send it WM_KEYDOWN messages.

您也可以通过 pywinauto 发​​送键盘输入。 a>或通过win32com AutoIt 的ActiveX控件。

You can also send keyboard input via pywinauto, or the ActiveX control of AutoIt via win32com.

使用AutoIt:

from win32com.client import Dispatch

auto = Dispatch("AutoItX3.Control")
auto.WinActivate("The window's title", "")
auto.WinWaitActive("The window's title", "", 10)

auto.Send("The input")

这篇关于控制Windows控制台应用程序w / stdin管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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