-1

I am tring to use a terminal application from python, after run app, this app ask to imput same words.

this is my curent code :

import pexpect
import sys

child = pexpect.spawn("./helium-wallet create basic --seed mobile")
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")
# don't warry about my seed, is just a example.

print(child.before)

After send words, app return this error :

(base) admin@192-168-0-181 helium-wallet-v1.6.10-x86-64-macos % python prova.py                           
Space separated seed words: b''

Look like python send a byte words, and not string ... How i can send string ?

Lucian Blaga
  • 137
  • 2
  • 7

1 Answers1

0

You never moved the pexpect cursor forward. sys.stdout.buffer echoed the prompt, so that is what before displayed. Comment out child.logfile_read = sys.stdout.buffer, and you will just get b''.

To move the buffer forward, you have two options:

NOTE - Tested on Ubuntu 20.04 using Python 3.8

1. If you do not have to enter any more data, use child.read() to let the program run; the cursor will automatically move forward. Once read encounters an EOF (program termination), logfile_read will display all the output to STDOUT:

Parent (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.")

Child (helium-wallet.py):

text = input("Child: Space separated seed words: ")
print("Child: Did something with:", text)
print("Child: Script complete. Have a nice day.")

Output:

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. If you have more data to enter (i.e., you need the child to stay alive), use child.expect() (I removed the logfile echo; using it with before clutters the output):

Parent (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.")

Child (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.")

Output:

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.

BTW, to avoid output clutter, I would use either the one-time child.logfile_read = sys.stdout.buffer, or print(child.before) after each expect, but not both. In your case, I suggest using child.before to capture output only.

Good luck coding!

Rob G
  • 673
  • 3
  • 10