Line buffering pipes

Shaun Jackman sjackman at gmail.com
Mon Nov 1 12:51:39 EST 2004


Thanks Chris and Alex,

tcpdump is buffering its output, and it seems the standard behaviour
of libc's stdio is to block buffer output going to a pipe. The FAQ
'Why not just use a pipe (popen())?' of the pexpect module addresses
this question thoroughly. Lucky for me and this particular case,
tcpdump provides a -l switch to line buffer the output even if it's
going to a pipe for the typical case of tcpdump -l | tee log.

I see that file.next() is also block buffering regardless of the
requested buffering method. I'd prefer to use the pretty form.
    for line in os.popen( 'tcpdump -l'): print line
However, the not-so-pretty form is required for line buffering.
    file = os.popen( 'tcpdump -l')
    while True: print file.readline()

Can file.next() be coaxed to behave as asked?

Please cc me in your reply. Thanks,
Shaun


> Then it looks like the buffering is being done by the application you're
> driving.  Try pexpect.sourceforge.net maybe...
> 
> Alex



More information about the Python-list mailing list