简体   繁体   English

Python pexpect sendline内容是否被缓冲?

[英]Python pexpect sendline contents being buffered?

I have the following code in Python using the pexpect module to run a perl script which asks a series of questions. 我在Python中使用以下代码使用pexpect模块运行一个perl脚本,该脚本询问了一系列问题。 I have used this for quite some time and works well until now. 我已经使用了很长一段时间,并且到目前为止效果很好。

pexpect python pexpect蟒蛇

index = child.expect(['Question 1:'])
os.system('sleep 2')
print 'Question 1:' + 'answer1'
child.sendline('answer1')

index = child.expect(['Question 2:'])
os.system('sleep 2')
print 'Question 1:' + 'answer2'
child.sendline('answer2')

index = child.expect(['Question 3:'])
os.system('sleep 2')
print 'Question 1:' + 'answer2'
child.sendline('answer2')

At this point, i have some code that checks if error will output when Question 2 & Question 3 does not match. 此时,我有一些代码可以检查当问题2和问题3不匹配时是否会输出错误。 I checked the pexpect log and the statements being sent are exactly what i want (number of bytes and string). 我检查了pexpect日志,发送的语句正是我想要的(字节数和字符串数)。

However when I check what gets accepted by the perl code its getting the following: 但是,当我检查被perl代码接受的内容时,将得到以下内容:

Question 1: 'answer 1' <-- CORRECT 问题1:“答案1” <-正确

Question 2: 'answer 1' <-- INCORRECT 问题2:“答案1” <-不正确

Question 3: 'answer 2answer 2' <-- its being combined into one for some reason 问题3:“答案2答案2” <-由于某种原因将其合并为一个

Any ideas out there? 有什么想法吗? I'm not using anything special when spawning my pexpect object: child=pexpect.spawn(cmd,timeout=140) 生成pexpect对象时,我没有使用任何特殊的东西: child=pexpect.spawn(cmd,timeout=140)

Edit: Added the perl code function that asks for Question 2 & 3 编辑:添加了要求问题2和3的perl代码功能

sub getpw 子getpw


sub getpw
{   my ($name, $var, $encoding) = @_;
    my $pw = $$var;

    PWD: while(1) {
        system("/bin/stty -echo");
        getvar($name, \$pw, 1);
        print "\n";
        system("/bin/stty echo");
        return if $pw eq $$var && length($pw) == 80;
        if (length($pw) > 32) {
            print STDERR "ERROR: Password cannot exceed 32 characters, please reenter.\n";
            next PWD;
        }

        return if $pw eq $$var;

        my $pw2;
        system("/bin/stty -echo");
        getvar("Repeat password", \$pw2, 1);
        print "\n";
        system("/bin/stty echo");
        print "#1: ";
        print $pw;
        print "\n";
        print "#2: ";
        print $pw2;
        if ($pw2 ne $pw) {
            print STDERR "ERROR: Passwords do not match, please reenter.\n";
            next PWD;
        }
        last;
    }

    # Escape dangerous shell characters
    $pw =~ s/([ ;\*\|`&\$!#\(\)\[\]\{\}:'"])/\\$1/g;

    my $correctlength=80;
    my $encoded=`$AVTAR --quiet --encodepass=$pw`;

    chomp($encoded);
    if($? == 0 && length($encoded) == $correctlength) {
        $$var = $encoded;
    } else {
        print "Warning: Password could not be encoded.\n";
        $$var = $pw;
    }
}

sub getvar 子getvar

sub getvar
{   my ($name, $var, $hide) = @_;
    my $default = $$var;
    while(1) {
        if($default) {
            $default = "*****" if $hide;
            print "$name [$default]: ";
        } else {
            print "$name: ";
        }
        my $val = <STDIN>;
        chomp $val;
        ### $val =~ s/ //g;  # do not mess with the password
        $$var = $val if $val;
        last if $$var;
        print "ERROR: You must enter a value\n";
    }
}

There is nothing wrong with your code on the python side. python端的代码没有错。 Check your perl side of the code. 检查代码的perl部分。 To demonstrate I have created below a python questions and answers scripts that call each other. 为了说明这一点,我在下面创建了一个python相互调用的问答脚本。 When you execute answers.py it will produce the expected output as below. 当您执行answers.py ,将产生预期的输出,如下所示。 Also you don't need to do the sleep statements child.expect will block until the expected output has been received. 另外,您不需要执行sleep语句child.expect就会阻塞,直到收到预期的输出为止。

questions.py Questions.py

answers = {}
for i in range(1, 4):
    answers[i] = raw_input('Question %s:' % i)
print 'resuls:'
print answers

answers.py answer.py

import pexpect
child=pexpect.spawn('./questions.py', timeout=140)
for i in range(1, 4):
    index = child.expect('Question %s:' % i)
    child.sendline('answer%s' % i)

child.expect('resuls:')
print child.read()

output 输出

{1: 'answer1', 2: 'answer2', 3: 'answer3'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM