Guest User

Untitled

a guest
Jul 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. from subprocess import *
  5.  
  6. #
  7. # subprocess.check_output() is new in Python 2.7
  8. #
  9. def _check_output(*popenargs, **kwargs):
  10. r"""Run command with arguments and return its output as a byte string.
  11.  
  12. If the exit code was non-zero it raises a CalledProcessError. The
  13. CalledProcessError object will have the return code in the returncode
  14. attribute.
  15.  
  16. The arguments are the same as for the Popen constructor. Example:
  17.  
  18. >>> check_output(["ls", "-l", "/dev/null"])
  19. 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
  20.  
  21. The stdout argument is not allowed as it is used internally.
  22. To capture standard error in the result, use stderr=STDOUT.
  23.  
  24. >>> check_output(["/bin/sh", "-c",
  25. ... "ls -l non_existent_file ; exit 0"],
  26. ... stderr=STDOUT)
  27. 'ls: non_existent_file: No such file or directory\n'
  28. """
  29. if 'stdout' in kwargs:
  30. raise ValueError('stdout argument not allowed, it will be overridden.')
  31. process = Popen(stdout=PIPE, *popenargs, **kwargs)
  32. output, unused_err = process.communicate()
  33. retcode = process.poll()
  34. if retcode:
  35. cmd = kwargs.get("args")
  36. if cmd is None:
  37. cmd = popenargs[0]
  38. raise CalledProcessError(retcode, cmd)
  39. return output
  40.  
  41. try:
  42. output = _check_output(sys.argv[1:], stdin=PIPE)
  43. print output,
  44. except KeyboardInterrupt:
  45. print 'Aborted.'
  46. except (OSError, CalledProcessError), e:
  47. print e
Add Comment
Please, Sign In to add comment