0

I am building an HTML document that is meant to run locally. On it is a button that I would like to have run a Python script when clicked. I'm trying to use a PHP-generated button. There's no input or output that I want to associate with the button; I just want it to run the script, which produces charts as image files that the rest of the page uses. I have tried a couple different ways to do it, including putting the PHP part in the HTML document, before the HTML:

<?php
    if (isset($_POST['update']))
    {
        exec('python myScript.py');
    }
>
<!DOCTYPE html>
<html>
<head>
</head> 
<body>

<form name="update" method="post" >
<button name = "update" type="submit"> Update charts </button>
</form>

</body>

I've also tried making the PHP code its own .php document in the same directory and calling it from the HTML code as so:

<form action = "updateCharts.php" method="post">
<input type="submit" name="update" />
</form>

where updateCharts.php is:

<?php
    system('cd C:\My\Script\Path');
    system('python MyScript.py');
?>

I've also tried substituting "system" with "exec" but to no avail. In the first part, I click the button and nothing happens. In the second, I click the button and I am taken to the text of the PHP document. In neither case does my Python script run!

Does anyone see what I'm missing? I'm admittedly a novice with php so it may be something glaring. Thanks for the help!

Apex7
  • 1
  • 1
  • 2
  • Where / how do you run the scripts? Is it your home server? Which software setup? See log files and post some of the latest content. – Pinke Helga May 16 '16 at 22:48
  • Have you tried getting the output and finding out what the problem is? – Chris May 16 '16 at 22:48
  • I've got some suspects but writing an answer without feedback wouldn't make sense. Your updateCharts.php can not work. – Pinke Helga May 16 '16 at 23:03
  • I am running everything on my PC. My html, php, and python script are all is C:\My\Script\Path. I'm able to manually run the command "python C:\My\Script\Path\myscript.py" from the command line and it runs flawlessly. Is that what you mean? Would you please explain why updateCharts.php can't work? – Apex7 May 16 '16 at 23:08
  • Yes, that's I mean. If you don't need to change work directory before script start, the simplest way is to call it by the absolute path as you did at the command line. Next isue is if apache may execute you script. I could tell you how to do in Linux, but check file permissions for apache user. – Pinke Helga May 16 '16 at 23:23
  • I changed it to an absolute reference just to be sure, still no luck. I'm running everything locally on my Windows 10 machine... where would apache come in? And how do I need to set the permissions? – Apex7 May 17 '16 at 00:06
  • On Linux Apache runs threads with "www-data" user privileges. Try if you can output the last row of `$out = system('dir');`. If not, Apache is not configured / allowed to do system calls at all. In my apache2.conf are the rows `User ${APACHE_RUN_USER}` and `Group ${APACHE_RUN_GROUP}`. In envvars is: `export APACHE_RUN_USER=www-data` and `export APACHE_RUN_GROUP=www-data`. If you find the system user name, manage the file permissions of the python script. – Pinke Helga May 17 '16 at 02:19

2 Answers2

0

What this:

$command = escapeshellcmd('python /My/Script/Path/myscript.py');
// or
// $command = escapeshellcmd('/My/Script/Path/test.py');
// But you have to make your script executable doing: chmod +x myscript.py
$output = shell_exec($command);
echo $output;
  • tried putting this in the if statement of the embedded version and in the lines of updateCharts.php and no luck :\ – Apex7 May 16 '16 at 23:05
  • What about this: $command = escapeshellcmd('C:\Python27\python C:\python_files\test.py'); $output = shell_exec($command); echo $output; – user6195996 May 16 '16 at 23:21
  • You need to escape strings only, when they are is unsafe user input, you don't want have executables inside, e.g. `'dir "/path/to/public/' . escapeshellcmd($unsafeInput) . '"'` – Pinke Helga May 16 '16 at 23:31
0

This can not work.

<?php
    system('cd C:\My\Script\Path');
    system('python MyScript.py');
?>

You can not have a "session" over multiple system calls. Every system call has its own shell environment.

At least on Linux systems you can cascade commands by semicolons. If you need a work directory to be set before script execution, you could either do

system('cd C:\My\Script\Path ; MyScript.py');

or

chdir('C:\My\Script\Path');
system('MyScript.py');

However, script execution from PHP can be blocked, or apache might not have appropriate file permissions to the script, or, or, or.

You need to check log files and also post what actually is output.

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
  • Apache? Is that running implicitly? Cuz I'm just running this all locally on my Windows 10 PC. It's a very basic project! Not sure where I should be looking for logs either. Is there code I should add to get it to generate logs? – Apex7 May 16 '16 at 23:58
  • @Apex7 - Ah, are you running IIS? Apache is a cross platform webserver. IIS is Microsoft's internet server. Refer http://www.iis.net/learn/manage/configuring-security/application-pool-identities in this case. – Pinke Helga May 17 '16 at 02:23