【问题标题】:python pexpect type string code i terminal apppython pexpect类型字符串代码我终端应用程序
【发布时间】:2021-12-01 00:08:02
【问题描述】:

我正在尝试使用 python 的终端应用程序, 运行应用程序后,此应用程序要求输入相同的单词。

这是我当前的代码:

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)

发送文字后,应用返回此错误:

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

看起来像python发送一个字节单词,而不是字符串...... 我如何发送字符串?

【问题讨论】:

    标签: python python-3.x pexpect


    【解决方案1】:

    您从未将 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.bufferprint(child.before),但不能同时使用两者。在您的情况下,我建议使用 child.before 仅捕获输出。

    祝你编码好运!

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多