Question about pipes/os.popen

K

Kevin Walzer

I'm trying to structure a Python script that streams output over a pipe.

Here is my code:

import os

cmd = os.popen('echo foo | sudo -S /usr/sbin/tcpdump -en1')
cmd.read()

This returns output of "". I'm expecting the standard output of "tcpdump
-en1". How does one read unbuffered output over a pipe before the pipe
is closed in Python? Because I want the output to be updated in real
time, writing to a temporary file and then reading that data isn't
feasible.

I'm not sure what I'm doing wrong here.
 
S

Steve Holden

Kevin said:
I'm trying to structure a Python script that streams output over a pipe.

Here is my code:

import os

cmd = os.popen('echo foo | sudo -S /usr/sbin/tcpdump -en1')
cmd.read()

This returns output of "". I'm expecting the standard output of "tcpdump
-en1". How does one read unbuffered output over a pipe before the pipe
is closed in Python? Because I want the output to be updated in real
time, writing to a temporary file and then reading that data isn't
feasible.

I'm not sure what I'm doing wrong here.
Probably expecting sudo to read the standard input for its password.

First of all, sudo doesn't always ask for your password. Secondly, when
it does I'm pretty sure it will take care to try and do it on the
controlling tty, not by reading stdin.

regards
Steve
 
K

Kevin Walzer

Steve Holden wrote:

Probably expecting sudo to read the standard input for its password.

First of all, sudo doesn't always ask for your password. Secondly, when
it does I'm pretty sure it will take care to try and do it on the
controlling tty, not by reading stdin.

sudo wasn't the problem. I've used a similar command structure (echo foo
| sudo -S command) to invoke sudo from a program.

Here's the code I used to get this running successfully:

---
import os


file = os.popen('echo foo| sudo -S /usr/sbin/tcpdump -v -i en1', 'r')


for line in file:
print line
---

I left out the "r" flag. D'oh.

I've gotten a bit lost among the various ways of invoking external
commands in Python (spawn, popen, system, subprocess). I'm glad os.popen
still works.
 
K

Kevin Walzer

Steve Holden wrote:

Probably expecting sudo to read the standard input for its password.

First of all, sudo doesn't always ask for your password. Secondly, when
it does I'm pretty sure it will take care to try and do it on the
controlling tty, not by reading stdin.

sudo wasn't the problem. I've used a similar command structure (echo foo
| sudo -S command) to invoke sudo from a program.

Here's the code I used to get this running successfully:

---
import os


file = os.popen('echo foo| sudo -S /usr/sbin/tcpdump -v -i en1', 'r')


for line in file:
print line
---

I left out the "r" flag. D'oh.

I've gotten a bit lost among the various ways of invoking external
commands in Python (spawn, popen, system, subprocess). I'm glad os.popen
still works.
 
D

Donn Cave

[ someone's command that fails, run in popen ]
Probably expecting sudo to read the standard input for its password.

Also probably having inflated expectations for popen(),
and other related functions for that matter. Where in
most Python library functions you can expect an exception
when something fails, that doesn't apply to failures in
shell commands. Not that it strictly couldn't be done,
but on the whole I haven't noticed that anyone cares.

Donn Cave, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top