Getting shell output using Python?

I have a shell script that receives whois information for domains and outputs takenor availableto the shell depending on the domain.

I would like to execute a script and be able to read this value inside my Python script.

I played with subprocess.call, but I can’t figure out how to get the result.

eg,

subprocess.call('myscript www.google.com', shell=True)

displays takenin the shell.

+3
source share
4 answers

subprocess.call()does not give you an exit, only a return code. For output you should use subprocess.check_output(). These are friendly wrappers around a family of functions that you can also use directly.

See http://docs.python.org/library/subprocess.html for details

+8

stdin stdout Popen , : communicate

:

p = subprocess.Popen(['myscript', 'www.google.com'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(stdoutdata, stderrdata) = p.communicate(input="myinputstring")
# all done!
+6
import subprocess as sp
p = sp.Popen(["/usr/bin/svn", "update"], stdin=sp.PIPE, stdout=sp.PIPE, close_fds=True)
(stdout, stdin) = (p.stdout, p.stdin)
data = stdout.readline()
while data:
    # Do stuff with data, linewise.

    data = stdout.readline()
stdout.close()
stdin.close()

I am using an idiom, obviously, in this case I was updating the svn repository.

+5
source

try it subprocess.check_output.

+1
source

Source: https://habr.com/ru/post/1785124/


All Articles