Using subprocess Module

From PeformIQ Upgrade
Revision as of 08:19, 15 September 2015 by PeterHarding (talk | contribs) (Created page with "=Source= Some examples of using the the subprocess module. <pre> #!/usr/bin/env python import subprocess def execute(command): tokens = command.split() print tok...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Source

Some examples of using the the subprocess module.

#!/usr/bin/env python


import subprocess

def execute(command):
    tokens = command.split()

    print tokens

    process = subprocess.Popen(tokens, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    output = ''

    # Poll process for new output until finished

    for line in iter(process.stdout.readline, ""):
        print line,
        output += line


    process.wait()

    exitCode = process.returncode

    if (exitCode == 0):
        return output
    else:
        raise Exception(command, exitCode, output)

execute('ping -c 23 127.0.0.1')