pexpect模块sendline方法的难点

发布于 2024-11-30 14:11:41 字数 607 浏览 3 评论 0原文

如果我期望面试问题中出现一些字符串,我需要通过给出预定义的答案来自动化面试过程。

我正在使用 pexpect.spawn 启动子进程并进行面试。

我的代码的一部分是:

child.expect("\> ")

child.sendline("test")

我使用 child.logfile=fout 将采访过程保存在文件中

fout 的一部分是:

What would you like to use for the display name:^M
> ^M

Invalid input: ''^M

^M

What would you like to use for the display name:^M
> test


test^M

如上所示,它第一次匹配新行,因此注释“无效输入:”。 所以它问同一个问题两次,然后接受“测试”作为答案。

我该怎么做才能让我的答案第一次被接受?

child.expect 中是否有与参数“timeout”相关的内容?

我尝试过使用 timeout=0、timeout=-1 等选项,但没有任何效果。

I need to automate a interview process by giving a predefined answers if I expect some string in the question of the interview.

I am using pexpect.spawn to start a child process and to go through the interview.

A portion of my code is :

child.expect("\> ")

child.sendline("test")

I am saving the interview process in a file using child.logfile=fout

A portion of fout is:

What would you like to use for the display name:^M
> ^M

Invalid input: ''^M

^M

What would you like to use for the display name:^M
> test


test^M

As seen above, it is matching a new line first time, hence the comment "Invalid input:".
SO it asks the same question twice and then it accepts "test" as the answer.

What can I do to make my answer accepted for the very first time ?

Is there anything related to the parameter "timeout" in child.expect ?

I have tried using options such as timeout=0, timeout=-1 etc. But nothing is working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2024-12-07 14:11:41

哇得到答案:
child.setecho(False)

在这个问题上花了几个小时。
幸运的是,这有效。

我认为问题在于,每当我们发送一行时,如果我们发送的行与我们期望的模式匹配(就像我们通常使用的 PROMPT 一样),那么在结果出来之前
希望回报。

不得不说我们必须仔细选择预期模式。
一般来说,对我来说,我遵循示例中推荐的方式,设置 PS1=[PEXPECT]\$ 然后 setecho off 然后发送命令来更改 PS1
这个程序对我有用。

wow get the answer:
child.setecho(False)

worked on this issue for couple hours.
luckily this works.

I think the issue lies in that whenever we send a line, if the line we sent matches the pattern we epxect (like the PROMPT we generally use. ) then before the result came out
pexpect returns.

have to say we have to choose the EXPECTING PATTERN CAREFULLY.
generally for me, I followed the recommended way in the example, setting PS1=[PEXPECT]\$ and then setecho off then send command to change the PS1
this procedure works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文