Control an external console program

Alexander Stante alex_stante at yahoo.de
Sun Aug 15 16:39:50 CEST 2004


Hi,

I know Python since about 3 days or so and I like it very much :-) I am
currently tying to control an external console driven program but I
have encountered some problems while trying to do so.

Suppose I will try to control a program like a ftp command line client.
At first I wrote something like:

import popen2

fin, fout = popen2.popen2("ftp")
print "Out: " + fin.readline()

I expected to get an output like "Out: ftp>" but I got not nothing. I
think the reason is that readline() reads a line till it encountered a
"\n" or EOF, but nothing of both happen because the ftp client just
wrote the prompt and is now waiting for input, or not? Therefore I
thought, ok I just write an "\n" to fout and because the ftp client
will echo it I can read a line with readline() and get the prompt. But
it doesn't work either. Now I started asking myself if it just work
this way at all. Instead of just writing a "\n" to fout I wrote
"help\n" and read a line from fin and this time I got the first line of
the output from the help command *yeeha* :-) Unfortunately any
successive read from fin does not work. I can only read the first line
the help command issued, not the 5 or 6 following the first line.

So my question is how do I control a command line tool? I want to
interactively control a command line tool, so I will issue some
commands need the output from the tool and act acording to it. I guess
readline() is not the answer because if there is no line to read
present it just waits. Can anyone give me some hints for writing such a
program? How is the python command line tool integrated in the many
IDE's around? I guess they have the same problem, or not?

Bye
Alex




More information about the Python-list mailing list