Pexpect:不能写信给STDIN——为什么?



为什么不起作用?

data, status = run('grep o', stdout=True, stdin='lol')
print(f'data: {data}, status: {status}')

完整代码:

import pexpect
import sys
def run(cmd, prompts=[], password=None, stdout=False, stdin=''):
"""runs and interacts with external commands"""
child = pexpect.spawn(cmd, encoding='utf-8')
for prompt in prompts:
try:
child.expect(prompt)
except pexpect.exceptions.TIMEOUT:
print(f'prompt "{prompt}" not matched')
sys.exit(1)
child.sendline(password)
child.send(stdin)
try:
data = child.read() if stdout else None
except pexpect.exceptions.TIMEOUT:
print('command took too long to print to stdout')
sys.exit(1)
child.close()
return data, child.exitstatus
data, status = run('grep o', stdout=True, stdin='lol')
print(f'data: {data}, status: {status}')

这是有效的:

def run(cmd, prompts=[], password=None, stdout=False, stdin=None):
"""runs and interacts with external commands"""
child = pexpect.spawn(cmd, encoding='utf-8')
for prompt in prompts:
try:
child.expect_exact(prompt)
except pexpect.exceptions.TIMEOUT:
err(f'prompt "{prompt}" not matched')
child.sendline(password)
if stdin is not None:
child.sendline(stdin)
child.sendeof()
child.sendeof()
try:
data = child.read() if stdout else None
except pexpect.exceptions.TIMEOUT:
err('command took too long to print to stdout')
exitstatus = child.wait()
return data, exitstatus

差异:

  • child.send(stdin)->child.sendline(stdin)
  • CCD_ 3被重复两次。这不是必需的具体示例(即运行grep o(,但显然对于scrypt enc - out.enc。我不知道为什么

我不知道为什么上面的任何一个都有效。如果你知道,请告诉我。

这并不重要,但我使用的是函数err,而不是printsys.exit:

def err(msg):
sys.stderr.write('{ERR}*{CLR} {}n'.format(msg, **THEME))
sys.stderr.flush()

最新更新