Naggappan Ramukannan
Naggappan Ramukannan

Reputation: 2812

Get only stdout in a variable in python using subprocess

I use the following command in cli as below,

  [mbelagali@mbelagali-vm naggappan]$ aws ec2 create-vpc --cidr-block 172.35.0.0/24 --no-verify-ssl --endpoint-url https://10.34.172.145:8787

/usr/local/aws/lib/python2.6/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:769: 
InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html

"Vpc": {
    "InstanceTenancy": "default",
    "State": "pending",
    "VpcId": "vpc-ebb1608e",
    "CidrBlock": "172.35.0.0/24",
    "DhcpOptionsId": "dopt-a24e51c0"
}

And now I redirect the warnings using "2>/dev/null" so that i get only the json response.

Now I need to implement this using the python subprocess and hence tried the following option,

cmd = "aws ec2 create-vpc --cidr-block " + cidr_block + " --no-verify-ssl --endpoint-url " + endpoint_url
cmd_arg = shlex.split(cmd.encode('utf-8'))
p1 = subprocess.Popen(
    cmd_arg,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT)
output, error = p1.communicate()

Now in output variable I get is complete output including the warning messages how can I ignore the warning message as I do it in the shell script

Upvotes: 0

Views: 1769

Answers (3)

jfs
jfs

Reputation: 414475

To get json data that the subprocess prints to its stdout while ignoring warnings on its stderr:

from subprocess import check_output

json_data = check_output(cmd, stderr=DEVNULL)

where DEVNULL is defined here.

Upvotes: 0

Eric Renouf
Eric Renouf

Reputation: 14510

If you don't want the stderr messages you should not have the flag stderr=subprocess.STDOUT which does the equivalent of 2>&1. If you just remove that I suspect you'll get what you want. If you want to redirect stderr to /dev/null you can follow this answer: How to hide output of subprocess in Python 2.7

Upvotes: 2

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23223

To separate stderr and stdout simply create two independent pipes.

p1 = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

To ignore stderr completely simply open devnull and redirect stderr there.

with open(os.devnull) as devnull:
    p1 = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=devnull)

os.devnull The file path of the null device. For example: '/dev/null' for POSIX, 'nul' for Windows. Also available via os.path.

Upvotes: 0

Related Questions