Simon
Simon

Reputation: 113

Python script does not execute from PHP invocation

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).

Upvotes: 1

Views: 121

Answers (2)

Mike Cronce
Mike Cronce

Reputation: 365

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.

Upvotes: 1

glenfant
glenfant

Reputation: 1318

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'

Upvotes: 0

Related Questions