user1891916
user1891916

Reputation: 1001

Unable to read response when Subprocess shell=False

In below code if execute in windows platform i am getting output

import subprocess
COMMAND = " Application.exe arg1 arg2"
process = subprocess.Popen(COMMAND, stdout=subprocess.PIPE, stderr=None, shell=True)

while process.poll() is None:
 output = process.stdout.readline()
 print output,

Output> Some text

But if i use shell=False I am not getting output how to get response in this case .

Upvotes: 1

Views: 430

Answers (1)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52143

When you set shell=False, you must provide COMMAND as list containing program name and arguments:

With shell=True:

COMMAND = "Application.exe arg1 arg2"

With shell=False:

COMMAND = ["Application.exe", "arg1", "arg2"]

I would recommend you to avoid using subprocess.Popen with shell=True in any case for security measures and use communicate() instead whenever possible:

>>> import subprocess
>>> COMMAND = " Application.exe arg1 arg2".strip().split()
>>> process = subprocess.Popen(COMMAND, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)

>>> output, error = process.communicate()
>>> print output

Upvotes: 2

Related Questions