Question

I have the following code in Python using the pexpect module to run a perl script which asks a series of questions. I have used this for quite some time and works well until now.

pexpect python

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. I checked the pexpect log and the statements being sent are exactly what i want (number of bytes and string).

However when I check what gets accepted by the perl code its getting the following:

Question 1: 'answer 1' <-- CORRECT

Question 2: 'answer 1' <-- INCORRECT

Question 3: 'answer 2answer 2' <-- its being combined into one for some reason

Any ideas out there? I'm not using anything special when spawning my pexpect object: child=pexpect.spawn(cmd,timeout=140)

Edit: Added the perl code function that asks for Question 2 & 3

sub 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

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";
    }
}
Was it helpful?

Solution

There is nothing wrong with your code on the python side. Check your perl side of the code. To demonstrate I have created below a python questions and answers scripts that call each other. When you execute answers.py it will produce the expected output as below. Also you don't need to do the sleep statements child.expect will block until the expected output has been received.

questions.py

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

answers.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'}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top