【问题标题】:Creating interactive options in pexpect在 pexpect 中创建交互式选项
【发布时间】:2018-02-28 23:03:51
【问题描述】:

这是一个有点笨拙的问题,因为我想不出一个好的方法来描述它,但希望你可以做这样的事情:

interact {
    \001 {do_something}
    \003 {do_something_else}
    "?" {
      set timeout 1
      expect_user {
                   "?" {send "?"}
                   timeout {send_user "show a menu of the things you can do"}
      }
      stty raw -echo
      set timeout 60
    }
    \035 {send "^]"
      send "quit\r"
      send_user "\n"
      exit
    }
  }

这将创建一个交互式会话,用户可以在其中照常进行业务,但在按下键盘组合时(ctrl+actrl+cctrl+e、?等)执行操作或显示描述可能的快捷方式的文本。

我正在尝试将一些脚本更新为 python 和 pexpect,但无法确定这在 pexpect 中是否可行。我尝试过使用输入过滤器,但似乎这不是真正“正确”的地方,或者我似乎无法找到任何好的例子。

@pynexj:试过你的脚本,虽然我没有从 ctrl 命令的标准输出上得到任何东西。

16:51:16 ~/scripts $ p3 testInputFilter.py | tee testInput.txt
16:51:19 ~/scripts $ ps u
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
usr 45721  0.0  0.0 109620  1876 pts/1    Ss   16:49   0:00 -ksh
usr 46622  0.0  0.0 108436  1776 pts/1    S    16:51   0:00 bash
usr 46734  5.5  0.0 135728  7688 pts/1    S+   16:51   0:00 python3.6 testI
usr 46735  0.0  0.0 100912   632 pts/1    S+   16:51   0:00 tee testInput.t
usr 46736  0.0  0.0 108336  1692 pts/5    Ss   16:51   0:00 /bin/bash --nor
usr 46759  0.0  0.0 110236  1132 pts/5    R+   16:51   0:00 ps u
16:51:21 ~/scripts $ ^C
16:51:42 ~/scripts $ exit
16:51:43 ~/scripts $ cat testInput.txt
16:51:19 ~/scripts $ ps u
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
usr 45721  0.0  0.0 109620  1876 pts/1    Ss   16:49   0:00 -ksh
usr 46622  0.0  0.0 108436  1776 pts/1    S    16:51   0:00 bash
usr 46734  5.5  0.0 135728  7688 pts/1    S+   16:51   0:00 python3.6 testI
usr 46735  0.0  0.0 100912   632 pts/1    S+   16:51   0:00 tee testInput.t
usr 46736  0.0  0.0 108336  1692 pts/5    Ss   16:51   0:00 /bin/bash --nor
usr 46759  0.0  0.0 110236  1132 pts/5    R+   16:51   0:00 ps u
16:51:21 ~/scripts $ ^C
16:51:42 ~/scripts $ exit
16:51:57 ~/scripts $

【问题讨论】:

标签: python expect pexpect


【解决方案1】:

参见以下示例(适用于 Python2Python3):

[STEP 114] # cat foo.py
import pexpect

def input_filter(s):
    if s == b'\x03':
        return b'\r: r u going to kill me? press ctrl-d to exit!\r'
    elif s == b'\x04':
        return b'\r: ok, bye; exit\r'
    else:
        return s

proc = pexpect.spawn('bash --norc')
proc.interact(input_filter=input_filter)
proc.expect(pexpect.EOF)
[STEP 115] # python foo.py
bash-4.4# ps                      <-- user input
   PID TTY          TIME CMD
 77616 pts/56   00:00:00 bash
 77617 pts/56   00:00:00 ps
bash-4.4#                         <-- press CTRL-C
bash-4.4# : r u going to kill me? press ctrl-d to exit!
bash-4.4#                         <-- press CTRL-D
bash-4.4# : ok, bye; exit
exit
[STEP 116] #

【讨论】:

  • 这是我使用它时的结果:
  • 谢谢,这在 Python 3 中现在可以工作了,尽管冒号操作符是一个特定于 shell 的东西。如果我尝试在其中打印一些东西,它直到 EOF 发生之后才会出现。 pexpect 有没有相当于 send_user 的?
  • 打印语句仅出现在 EOF 之后,而不是在交互式会话期间。如果根据我最初的问题,我想向我的用户显示一个 ctrl+修饰符命令菜单,打印是不够的,返回一个字符串只会将该字符串发送到生成的孩子。
  • 没问题。这是与示例合影的new question
猜你喜欢
  • 2020-08-17
  • 2021-08-23
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多