使用 cmd 进程 python
Using cmd processes python
我正在尝试让我的基本 python 脚本打开并写入和读取 cmd,以便允许它向 noxplayer 发送命令。我正在尝试使用子流程,并且已经发现并阅读了有关使用 PIPES 而不是使用它们的信息。无论哪种方式,我都可以让它发送输入,从而产生一系列不同的断点。这是我尝试过的 2 个代码示例。
class bot:
def __init__(self, num):
self.num = num
def start(self):
#open app and command prompt
androidArg1 = "C:/Program Files (x86)/Nox/bin/Nox.exe"
androidArg2 = "-clone:Nox_" + str(self.num)
androidArgs = [androidArg1, androidArg2]
cmdArg1 = 'cmd'
cmdArgs = [cmdArg1]
self.app = subprocess.Popen(androidArgs)
self.cmd = subprocess.Popen(cmdArgs, shell=True)
self.cmd.communicate(input="cd C:/Program Files (x86)/Nox/bin")
while True:
self.devices = self.cmd.communicate(input="nox_adb devices")
print(self.devices)
正在打印 C:\Users\thePath>
但从未完成第一次通信
class bot:
def __init__(self, num):
self.num = num
def start(self):
#open app and command prompt
androidArg1 = "C:/Program Files (x86)/Nox/bin/Nox.exe"
androidArg2 = "-clone:Nox_" + str(self.num)
androidArgs = [androidArg1, androidArg2]
cmdArg1 = 'cmd'
cmdArgs = [cmdArg1]
self.app = subprocess.Popen(androidArgs)
self.cmd = subprocess.Popen(cmdArgs, stdin=PIPE, stderr=PIPE, stdout=PIPE, universal_newlines=True, shell=True)
stdout, stderr = self.cmd.communicate()
stdout, stderr
self.cmd.communicate(input="cd C:/Program Files (x86)/Nox/bin")
while True:
self.devices = self.cmd.communicate(input="nox_adb devices")
print(self.devices)
投掷
Cannot send input after starting communication
我做错了什么,正确的做法是什么?
communicate
很棒,因为它能够分别读取标准输出和错误。
但除此之外它非常笨重,因为它只发送一次输入。所以一旦发生这种情况:
stdout, stderr = self.cmd.communicate()
结束了,您不能向您的进程发送更多输入。
另一种方式是:
- 将输入逐行提供给进程
- 合并标准输出和错误(避免死锁)
但在这里就有点矫枉过正了。首先,在 Windows cmd
过程中 Popen
根本不需要 就有些矫枉过正了。加上 cd
命令,加上输入提要,加上 shell=True
...
简单点
相反,直接在循环中的 nox
命令上使用 Popen
(调用之间可能有一小段延迟)。
我没有对此进行测试,但这是一种自包含的方式,可以在给定目录中重复 运行 带有参数的命令,并读取其输出。
import time,subprocess
while True:
p = subprocess.Popen(["nox_adb","devices"],cwd="C:/Program Files (x86)/Nox/bin",stdout=subprocess.PIPE)
devices = p.stdout.read().decode()
rc = p.wait() # wait for process to end & get return code
if rc:
break # optional: exit if nox_adb command fails
time.sleep(1)
如果 nox_adb
是一个不会剪切它的 .bat
文件,在这种情况下,在命令前加上 cmd /c
:
p = subprocess.Popen(["cmd","/c","nox_adb","devices"],cwd="C:/Program Files (x86)/Nox/bin",stdout=subprocess.PIPE)
这大致相当于在 Windows 上添加 shell=True
,但是 shell=True
是解决多年后像回旋镖一样回到你脑海中的问题的一种懒惰方式,所以更好在工业解决方案中避免它。
我正在尝试让我的基本 python 脚本打开并写入和读取 cmd,以便允许它向 noxplayer 发送命令。我正在尝试使用子流程,并且已经发现并阅读了有关使用 PIPES 而不是使用它们的信息。无论哪种方式,我都可以让它发送输入,从而产生一系列不同的断点。这是我尝试过的 2 个代码示例。
class bot:
def __init__(self, num):
self.num = num
def start(self):
#open app and command prompt
androidArg1 = "C:/Program Files (x86)/Nox/bin/Nox.exe"
androidArg2 = "-clone:Nox_" + str(self.num)
androidArgs = [androidArg1, androidArg2]
cmdArg1 = 'cmd'
cmdArgs = [cmdArg1]
self.app = subprocess.Popen(androidArgs)
self.cmd = subprocess.Popen(cmdArgs, shell=True)
self.cmd.communicate(input="cd C:/Program Files (x86)/Nox/bin")
while True:
self.devices = self.cmd.communicate(input="nox_adb devices")
print(self.devices)
正在打印 C:\Users\thePath>
但从未完成第一次通信
class bot:
def __init__(self, num):
self.num = num
def start(self):
#open app and command prompt
androidArg1 = "C:/Program Files (x86)/Nox/bin/Nox.exe"
androidArg2 = "-clone:Nox_" + str(self.num)
androidArgs = [androidArg1, androidArg2]
cmdArg1 = 'cmd'
cmdArgs = [cmdArg1]
self.app = subprocess.Popen(androidArgs)
self.cmd = subprocess.Popen(cmdArgs, stdin=PIPE, stderr=PIPE, stdout=PIPE, universal_newlines=True, shell=True)
stdout, stderr = self.cmd.communicate()
stdout, stderr
self.cmd.communicate(input="cd C:/Program Files (x86)/Nox/bin")
while True:
self.devices = self.cmd.communicate(input="nox_adb devices")
print(self.devices)
投掷
Cannot send input after starting communication
我做错了什么,正确的做法是什么?
communicate
很棒,因为它能够分别读取标准输出和错误。
但除此之外它非常笨重,因为它只发送一次输入。所以一旦发生这种情况:
stdout, stderr = self.cmd.communicate()
结束了,您不能向您的进程发送更多输入。
另一种方式是:
- 将输入逐行提供给进程
- 合并标准输出和错误(避免死锁)
但在这里就有点矫枉过正了。首先,在 Windows cmd
过程中 Popen
根本不需要 就有些矫枉过正了。加上 cd
命令,加上输入提要,加上 shell=True
...
简单点
相反,直接在循环中的 nox
命令上使用 Popen
(调用之间可能有一小段延迟)。
我没有对此进行测试,但这是一种自包含的方式,可以在给定目录中重复 运行 带有参数的命令,并读取其输出。
import time,subprocess
while True:
p = subprocess.Popen(["nox_adb","devices"],cwd="C:/Program Files (x86)/Nox/bin",stdout=subprocess.PIPE)
devices = p.stdout.read().decode()
rc = p.wait() # wait for process to end & get return code
if rc:
break # optional: exit if nox_adb command fails
time.sleep(1)
如果 nox_adb
是一个不会剪切它的 .bat
文件,在这种情况下,在命令前加上 cmd /c
:
p = subprocess.Popen(["cmd","/c","nox_adb","devices"],cwd="C:/Program Files (x86)/Nox/bin",stdout=subprocess.PIPE)
这大致相当于在 Windows 上添加 shell=True
,但是 shell=True
是解决多年后像回旋镖一样回到你脑海中的问题的一种懒惰方式,所以更好在工业解决方案中避免它。