[Twisted-Python] How to emulate Python's getoutput function

Jp Calderone exarkun at divmod.com
Fri Oct 7 23:35:29 EDT 2005


On Fri, 07 Oct 2005 23:04:01 -0400, Drake Smith <drakesmith at adelphia.net> wrote:
>Is there a relatively simple way to emulate the following python function:
>   commands.getoutput ('ifconfig eth0 | grep inet'))

For the particular case of grep, this is pretty straightfoward:

    def gotOutput(output):
        lines = output.splitlines()
        for L in lines:
            if 'inet' in L:
                yield L
    utils.getProcessOutput(...).addCallback(gotOutput).addCallback(something)

For the general case of chaining processes, you need to get down to reactor.spawnProcess().  This API lets you specify which file descriptors are connected to what.  A pipe ends up being just that - use os.pipe() to create one, hand one end to the stdout of one process and the other end to the stdin of another, and now they're talking :)

Hope this helps,

Jp




More information about the Twisted-Python mailing list