Kodi Community Forum
Getting stdout from external app - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32)
+--- Forum: Add-ons (https://forum.kodi.tv/forumdisplay.php?fid=26)
+--- Thread: Getting stdout from external app (/showthread.php?tid=60466)



Getting stdout from external app - Dortamur - 2009-10-29

I'm writing an XBMC script, and for part of it, I need to run a command-line utility and get back the output from the utility.

Here's the method I tried first:
Code:
out = os.popen("/external/cmd -arg").read();
However, this method kills the XBMC script and the debug log says:
Code:
OSError: (0,'Error')

So I then decided to try this method:
Code:
out = subprocess.Popen(["/external/cmd -arg"], shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
This time, the debug info on crash was:
Code:
OSError: [Errno 10] No child processes

Now, both these first two methods ran just fine from a test script (using Ubuntu's native Python) - but both failed within Xbmc.

So I resorted to this:
Code:
os.system("/external/cmd -arg > /tmp/temp.out");
out = open("/tmp/temp.out").read();

This works within Xbmc. Hurrah!
- but it's a hack! Eek

Why won't my first two techniques work? Is there any way to run a command-line tool and directly capture the stdout, without resorting to temp files?

My XBMC platform is PC running Ubuntu Jaunty, and my currently installed xbmc is 9.04.3+svn23754-jaunty1.

Thanks.


- necroys - 2009-12-25

Code:
def getstdout(aCmdline):
  process = subprocess.Popen(aCmdline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
  return process.stdout.read()