Interfacing with terminal programs

Donn Cave donn at oz.net
Sun May 13 23:54:59 EDT 2001


Quoth drifty at bigfoot.com (Drifty):
| For instance, a wrote a small Python script that basically prompted
| for info, printed some text, and then ended.  When I tried using
| popen2 on it I couldn't read the prompt.  It always hung.  Now a post
| said that I should flush it first.  I vaguely remember trying that and
| still having a problem, but I will give that a go just to
| double-check.

If your only problem is just that you can't read the prompt, that
may be easily solved after all.  I forgot about this one, because
I always use the system I/O file descriptor or unit number when
dealing with pipes, but you're probably reading from the file object
that popen2 returns.  The file object supports the lovely C stdio
API, which gives you two options:  read a complete line (you don't
want that, since a prompt has no newline and therefore is not a line),
or read some number of bytes (and you would rather not have to know
a priori how long the prompt may be.)  In either case, if you do the
obvious thing, the read will hang.

If you use the file object's fileno() method to get the file descriptor,
you can forget about the file object and use os.read(fd, size) to
read what's in the pipe.

	Donn Cave, donn at oz.net



More information about the Python-list mailing list