【问题标题】:pexpect output not showingpexpect 输出不显示
【发布时间】:2013-02-23 15:17:47
【问题描述】:

我们简单的 pexpect 脚本是这样的:

import pexpect
import sys

test = pexpect.spawn('ftp www.today.com')
test.logfile = sys.stdout
test.expect('Name.*')

但是,在调用脚本的 shell 上,没有显示输出。 相反,它似乎挂起,但我们可以看到进程 ftp ... 已生成。

如何在调用脚本的 shell 上显示输出?

谢谢

【问题讨论】:

    标签: python pexpect


    【解决方案1】:

    您可能需要使用logfile_read。代码如下:

    import pexpect
    import sys
    test = pexpect.spawn('ftp www.today.com')
    test.logfile_read = sys.stdout
    test.expect('Name.*')
    

    【讨论】:

    • .logfile_read 显示 less 输出(不包括 _send)而不是问题中显示的 .logfile
    • 从问题看来,输出似乎需要知道程序是否被调用,然后它被卡在哪里。所以在这种情况下,_read 会有所帮助。我尝试了.logfile,但没有看到脚本被卡住的问题中提到的任何输出
    • test.logfile contains both input and output。如果您对.logfile 什么也看不到; .logfile_read 你也看不到任何东西
    【解决方案2】:

    test.logfile 将只包含命令的输出,命令行本身并没有记录在 logfile 属性中。

    因此,只要生成命令并且没有输出,调用脚本时外壳中不会显示任何内容。 例如,当 ftp 连接超时时会有一个显示。

    【解决方案3】:

    这行应该:

    test = pexpect.spawn('ftp www.today.com')
    

    不是:

    test = pexpect.spawn('ftp ftp.today.com')
    

    因为通常如果你想要ftp,你必须使用ftp.something.com

    【讨论】:

    • 这个答案与问题无关。 FTP 服务器可以在任何主机名上运行,而不仅仅是以字母 ftp 开头的主机名
    • @DanielLawson: ftp vs. www 是这里的核心问题。 ftp www.today.com 导致超时,而ftp ftp.today.com 显示ftp> 提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多