1

I am trying to execute a Python Script as a response to my PHP code. This is what my Python Script looks like (for testing purposes ofcourse):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
print 'test'

import sys
sessionDir = sys.argv[1][:-14]
sys.stdout = open(sessionDir + 'output.txt','w')

print sessionDir

sys.stdout.close()

My PHPCode looks like the following:

global $calculationDir, $calculation, $userFilesDir;
//$pythonExe = '"D:\\WinPython-64bit-2.7.10.3\\WinPython Command Prompt.exe" python ';
//$pythonExe = 'abaqus python ';
$pythonExe = 'python ';
$scriptFile = '"'.$_SERVER['APPL_PHYSICAL_PATH'].substr($calculationDir,2).$calculation.'\\python\\test.py" ';
$dataFile = '"'.$userFilesDir.$_SESSION['user'].'\\'.$calculation.'\\'.$this->sessionID.'\\Parameters.dat"';
$command = escapeshellcmd($pythonExe.$scriptFile.$dataFile);

echo passthru('ipconfig').'<br>';

$output = passthru($command);
echo $output.'<br>';

The passthru($command) does not execute the python script. The command line works just fine if I type it in the command console by hand. For testing purposes I also print my ipconfigjust to test if the function works - which it does.

As you can see in my code I have three approaches to call the python intepreter. First is a portable python version, second is abaqus python and third is the systempath to python. And here is the tricky part. The script does work with abaqus python but neither of the other two pythons.

I also tried exec(), shell_exec(), system() instead of passthru(), which did not change anything. The users are also granted full access on the python script. What am I missing? Do you have any Ideas what else I should try to see if the python script is executed? (Is there a lack of information you need me to provide?)


Edit:

It runs on an IIS 7 Web-Server. - I changed the permission for the python Script via the Windows File-Explorer and also via the IIS-Programm (for the whole folder).

2 Answers 2

1

This isn't super helpful as an answer, but I don't have enough rep to comment. Hopefully somebody can build on it and provide a final solution, but this will at least give you a little direction.

Since you're echoing HTML, I'm making an assumption that you're running this PHP code from an HTTP request.

Most likely is that the user that your PHP code is being run as (e.g. apache - it depends on your server config) doesn't have permission to execute your Python script.

It looks like you're on Windows, though, from your use of backslashes as a directory separator, and I don't know how to fix that on Windows.

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

3 Comments

You are right, on all your assumptions. I tried giving the permission by changing the files security to all access. Is this not enough? - Or asked the other way around: Does this work?
I don't have enough Windows experience to say whether or not that will work or how to do it :( I can tell you that on a Linux system I would chmod 755 the script to test and see if it would work, which should be roughly equivalent to 'all access', but like I said, I'm not nearly familiar enough with the idiosyncrasies of Windows administration to say
I did some research on the file security properties and changed it accordingly. It did not help in the beginning, but somhow the magic restart the machine (not only the server) did the trick.
0

Provide/compute absolute full paths to executable and files. Your PHP process may not run under the same environment as your Powershell or console. i.e. :

$pythonExe = 'C:\\Python27\\python.exe'

1 Comment

Isn't this what I did in my first attempt?: $pythonExe = '"D:\\WinPython-64bit-2.7.10.3\\WinPython Command Prompt.exe" python ';

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.