473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Question about pipes/os.popen

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.

--
Kevin Walzer
Poetic Code
http://www.kevin-walzer.com
Sep 15 '06 #1
4 2037
Kevin Walzer wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 15 '06 #2
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.

--
Kevin Walzer
Poetic Code
http://www.kevin-walzer.com
Sep 15 '06 #3
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.

--
Kevin Walzer
Poetic Code
http://www.kevin-walzer.com
Sep 15 '06 #4
In article <ma**************************************@python.o rg>,
Steve Holden <st***@holdenweb.comwrote:

[ someone's command that fails, run in popen ]
cmd.read()

This returns output of "".
....
I'm not sure what I'm doing wrong here.
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, do**@u.washington.edu
Sep 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jane Austine | last post by:
I need to control a command line program via python. I first tried popen2 and 3 but I couldn't find a way to talk to the subprocess interactively; that is, read some and then write some, and...
2
by: Tim Black | last post by:
In my recent experience, popen os pipes always fail when cwd is a UNC path. Can anyone shed any light on this? Although I've seen lots of UNC path-related problems in this newsgroup, I've not been...
5
by: Peng Yuan Fan | last post by:
Hi, sorry if you found I have multi posted in different groups. It didn't appear in that group. I am trying to write an automated testing program with CxxTest, which in turn needs to talk to gdb...
11
by: Frank Millman | last post by:
Hi all I understand that StringIO creates a file-like object in memory. Is it possible to invoke another program, using os.system() or os.popen(), and use the < redirect operator, so that the...
0
by: E. Robert Tisdale | last post by:
Madhusudan Singh wrote: > Just a basic question from someone who is new to pipes. > > The statement of the problem is simple : > > Instead of using popen(progname,"w"), > I wish to use the...
5
by: Dara Durum | last post by:
Hi ! I want to create a Process Pool Object. I can hold started processes, and can communicate with them. I tryed with many ipc methods, but every of them have bug or other problem. Sockets...
1
by: sven _ | last post by:
Keywords: subprocess stdout stderr unbuffered pty tty pexpect flush setvbuf I'm trying to find a solution to <URL:http://bugs.python.org/issue1241>. In short: unless specifically told not to,...
40
by: Phlip | last post by:
Scott Lurndal wrote: Would it also cause a problem if your file descriptors go "around the horn", and exceed some platform-specific limit, such as 0x80000000? They would "go negative" when...
2
by: Gabriel Genellina | last post by:
En Tue, 19 Aug 2008 16:48:26 -0300, aditya shukla <adityashukla1983@gmail.comescribi�: I don't have a Vista machine to test right now, but I'd expect the above sequence to hang indefinitely....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.