sous-processus.Popen: http://docs.python.org/2/library/subprocess.html#subprocess.Popen
import subprocess
command = "ntpq -p" # the shell command
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True)
#Launch the shell command:
output = process.communicate()
print output[0]
Dans le Popen constructeur, si la coquille est Vrai, vous devez passer une commande en tant que chaîne de caractères, plutôt que comme une séquence. Sinon, il suffit de fractionner la commande en une liste:
command = ["ntpq", "-p"] # the shell command
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None)
Si vous avez besoin de lire également la norme de l'erreur, dans le Popen d'initialisation, vous pouvez définir stderr de sous-processus.De la PIPE ou de sous-processus.STDOUT:
import subprocess
command = "ntpq -p" # the shell command
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
#Launch the shell command:
output, error = process.communicate()