0

I can get my script to run, but it won't run correctly and I'm very confused. I'm trying to create a website where a user clicks a button, and this causes a python script on the server to start running. The script is supposed to be able to open a csv file, and work with the data stored in it. This script works if I run it normally from the command line. It is able to open the csv files, and if the file doesn't exist, gives me an error like this:

Traceback (most recent call last):
File "LaserProfile.py", line 224, in electrodeData
data = np.loadtxt(filepath, delimiter = ",", skiprows = 2)
File "C:\Anaconda\lib\site-packages\numpy\lib\npyio.py", line 734, in loadtxt
fh = iter(open(fname, 'U'))
IOError: [Errno 2] No such file or directory: '\\\\172.16.113.55\\file_root\\U1\\2015\\03\\566\\Laser\\15997.csv'

Basically, runs exactly the way it should. However, when the python script is told to execute by the PHP script, it seems to run fine EXCEPT it loses all capability to open these csv files, for each one returning an error like this:

Traceback (most recent call last): 
File "M:\GitHub\ETL\LaserProfile.py", line 224, in electrodeData 
data = np.loadtxt(filepath, delimiter = ",", skiprows = 2) 
File "C:\Anaconda\lib\site-packages\numpy\lib\npyio.py", line 734, in loadtxt 
fh = iter(open(fname, 'U')) 
IOError: [Errno 22] invalid mode ('U') or filename: '\\\\172.16.113.55\\file_root\\U1\\2015\\04\\617\\Laser\\16687.csv' 

In both cases, the script is being run from the same computer (Windows Server 2012). I don't see why I'm getting Errno22 here (especially since this is a file that processed correctly when running directly from the command line).

If it helps, here is my PHP:

<?php
    $pyscript = $_REQUEST["q"];
    $clientIP = $_SERVER['REMOTE_ADDR'];
    $basepath = "M:\\GitHub\\ETL\\";
    $pypath = $basepath.$pyscript;
    $hostIP = "172.16.111.45";
    $cmd = "python $pypath $clientIP";
    echo "Executing: $cmd <br /> Result: <br />";
    $result = `$cmd`;
    echo $result;
?>

And I'm running it from the command line with python LaserProfile.py (The script is not utilizing any parameters in sys.argv yet).

Why is it running fine from the command line but not from PHP?

1 Answers1

1

You can execute like so:

$command = escapeshellcmd('python $pypath $clientIP');
$output = shell_exec($command);
echo $output;

There is further information Running a python script from PHP

Community
  • 1
  • 1
The Humble Rat
  • 4,586
  • 6
  • 39
  • 73