2

The code is as follows:

    fh = tempfile.NamedTemporaryFile(delete=False,suffix = '.py')
    stream = io.open(fh.name,'w',newline='\r\n')
    stream.write(unicode(script))
    stream.flush()
    stream.close()
    proc = subprocess.Popen(
        [path,fh.name], 
        shell=True,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    proc.stdin.close()
    proc.stderr.close()
    out = proc.stdout.readline()
    print out

script is a string which contains the subprocess code, in this case a simple hello world. Since it has unix file endings, I had to use io.open in order to write it properly for windows. path is the path to the python.exe on my machine. The file is generated and looks fine in notepad:

    def main():
        print 'hello world'

However, when I run the program, the subprocess executes and does nothing. Its not a problem with the executable path, I've tested it with other programs, so it must be with either the temp file itself, or the text within it. Delete is set to false in order to check the contents of the file for debugging. Is there anything glaringly wrong with this code? I'm a bit new to using Popen.

1 Answer 1

1

The main issue in your program is that when you specify shell=True , you need to provide the entire command as a string, not a list.

Given that, there is really no need for you to use shell=True , also, unless absolutely neccessary, you should not use shell=True , its a security hazard, this is given in the documentation as well -

Executing shell commands that incorporate unsanitized input from an untrusted source makes a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution. For this reason, the use of shell=True is strongly discouraged in cases where the command string is constructed from external input:

Also, if you do not want to use stdin / stderr (since you are closing those off as soon as you start the process) , there is no need to use PIPE for them.

Example -

fh = tempfile.NamedTemporaryFile(delete=False,suffix = '.py')
stream = io.open(fh.name,'w',newline='\r\n')
stream.write(unicode(script))
stream.flush()
stream.close()
proc = subprocess.Popen(
    [path,fh.name], 
    stdout=subprocess.PIPE,
)
out = proc.stdout.readline()
print out

Also, the script -

def main():
    print 'hello world'

would not work, since you need to call main() for it to run.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok so update: I pasted the temporary file's contents into a new file and tried to execute it through the Python IDE, and nothing happened as well, the program terminated with no output. This leads me to believe that there is a problem with the script translation from unix to windows
Yea, you are not calling main() in the script?
The IDE im working with automatically calls main when it runs a script
not python.exe , ide may call, python.exe would not. Try calling it at the last line of the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.