您从未将 pexpect 光标向前移动。 sys.stdout.buffer 回应了提示,所以这就是 before 显示的内容。注释掉child.logfile_read = sys.stdout.buffer,你会得到b''。
要向前移动缓冲区,您有两种选择:
注意 - 使用 Python 3.8 在 Ubuntu 20.04 上测试
1. 如果您不必再输入任何数据,请使用child.read() 让程序运行;光标会自动向前移动。一旦read 遇到EOF(程序终止),logfile_read 将显示所有输出到 STDOUT:
父级(parent.py):
import pexpect
import sys
# helium-wallet.py is just a quick script I wrote to mimic your script
print("Parent: Running the child.")
child = pexpect.spawn("python3 helium-wallet.py")
child.logfile_read = sys.stdout.buffer
child.expect("Space separated seed words: ")
child.sendline("carpet again stick economy finish recipe blouse forward program stumble need ginger")
child.read()
print("Parent: Script complete. Have a nice day.")
子 (helium-wallet.py):
text = input("Child: Space separated seed words: ")
print("Child: Did something with:", text)
print("Child: Script complete. Have a nice day.")
输出:
Parent: Running the child.
Child: Space separated seed words: carpet again stick economy finish recipe blouse forward program stumble need ginger
Child: Did something with: carpet again stick economy finish recipe blouse forward program stumble need ginger
Child: Script complete. Have a nice day.
Parent: Script complete. Have a nice day.
2. 如果您要输入更多数据(即,您需要孩子活着),请使用child.expect()(我删除了logfile 回显;将其与before 一起使用使输出混乱):
父级(parent.py):
import pexpect
# helium-wallet.py is just a quick script I wrote to mimic your script
print("Parent: Running the child.")
child = pexpect.spawn("python3 helium-wallet.py")
child.expect("Space separated seed words: ")
child.sendline("carpet again stick economy finish recipe blouse forward program stumble need ginger")
child.expect("Requesting more data: ")
# Now you can use before to see the results of the first input,
# since you moved the cursor forward with pexpect.expect.
print()
print("First input results:")
print("Before:", child.before)
print("Match:", child.match)
print("After:", child.after)
print()
child.sendline("Here's more data!")
output = child.read()
print("Remaining output (including your input):\n", output.decode())
print("Parent: Script complete. Have a nice day.")
子 (helium-wallet.py):
text = input("Child: Space separated seed words: ")
print("Child: Did something with:", text)
text2 = input("Child: Requesting more data: ")
print("Child: Did something else with:", text2)
print("Child: Script complete. Have a nice day.")
输出:
Parent: Running the child.
First input results:
Before: b'carpet again stick economy finish recipe blouse forward program stumble need ginger\r\nChild: Did something with: carpet again stick economy finish recipe blouse forward program stumble need ginger\r\nChild: '
Match: <re.Match object; span=(204, 226), match=b'Requesting more data: '>
After: b'Requesting more data: '
Remaining output (including your input):
Here's more data!
Child: Did something else with: Here's more data!
Child: Script complete. Have a nice day.
Parent: Script complete. Have a nice day.
顺便说一句,为了避免输出混乱,我会在每个expect 之后使用一次性child.logfile_read = sys.stdout.buffer 或print(child.before),但不能同时使用两者。在您的情况下,我建议使用 child.before 仅捕获输出。
祝你编码好运!