Skip to content

Instantly share code, notes, and snippets.

@gustavorv86
Last active June 19, 2019 10:41
Show Gist options
  • Save gustavorv86/43e90e8059ceea9acb095c5118e781c5 to your computer and use it in GitHub Desktop.
Save gustavorv86/43e90e8059ceea9acb095c5118e781c5 to your computer and use it in GitHub Desktop.
Execute command in Python and get stdout, stderr and return code.
#!/usr/bin/env python
import subprocess
def execute_process(cmd) :
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
return {'stdout': out, 'stderr': err, 'retcode': p.returncode}
if __name__ == "__main__":
ret = execute_process('ping -w 3 www.google.com')
print('Done')
print('STDOUT: ' + ret['stdout'])
print('STDERR: ' + ret['stderr'])
print('RETCODE: ' + str(ret['retcode']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment