dubbaluga
dubbaluga

Reputation: 2343

Command-output (stdout, stderr) to the web in Python

Recently I have developed a short Python script using the Flask framework to start a process over HTTP (see here). Now there is the question for an adequate technology to "pipe" the standard streams (I am especially interested in stdout and stderr) of started processes to the Web.

What would you say is the most suitable way to accomplish this? First I thought of websockets to fit perfectly. But then it seemed to me that most implementations veil their connected clients. I think that I need this information in order to be able to send the output to all of them.

Edit: I think that my question was a bit unclear: I want to see the output of the executed command in a web-interface. So I have to "convey" data from stdout/stderr of the executed process via HTTP somehow to the web-interface(s). It might be possible that the started command may run for some (longer) time. The example which invokes the dd command is not representative as it does not output anything in the given setting. And sure, I would have to use the output of the subprocess.Process class and objects (e. g. by using the communicate() method).

Upvotes: 0

Views: 617

Answers (2)

oberstet
oberstet

Reputation: 22051

One approach that will work (and is non-blocking, can serve multiple clients) is using Twisted:

Connect a ProcessProtocol

http://twistedmatrix.com/documents/current/core/howto/process.html

to a Twisted WebSocket server

https://github.com/tavendo/AutobahnPython

Disclosure: I am author of Autobahn.

Upvotes: 1

John Mee
John Mee

Reputation: 52313

Not very clear what your goal is. Your code has this. Perhaps that's a clue?

   current_app.process = subprocess.Popen(
            ['dd', 'if=/dev/zero', 'of=/dev/null'])

Trawl through the docs for subprocess. You'll find you can specify stdout and stderr. http://docs.python.org/library/subprocess.html#module-subprocess

One strategy might be to capture stdout/err to file then rewrite that file to your http response.

Upvotes: 1

Related Questions