0

This is a follow up question to one that I posted earlier.

I have a html button that I would like to run a python script:

<form name = "input" action = "script.php" method = "get">
<input type= "submit" name = "submit" value="Submit" />
</form>

And the php it accesses looks like:

<?php exec("python /c/wamp/www/thursday.py");?>

My problem is that the code is not executing, and I am not receiving anything in my error log. What am I doing wrong?

My python code organizes images in a folder and asks for user input if the file is named incorrectly according to the way it organizes.

Will the python code still ask for input if it is run from a web server?

Sources I've looked at: Create HTML button to run Python Script and run python script with html button

Community
  • 1
  • 1
Alina DW
  • 111
  • 1
  • 17
  • I use GM https://stackoverflow.com/questions/15040849/how-to-send-data-from-greasemonkey-to-a-php-wamp-server/47493014#47493014 – Dave B Nov 29 '17 at 09:38

1 Answers1

3

Will the python code still ask for input if it is run from a web server?

Yes and no. When the Python script is executed and it prompts for input, then yes, it will wait for input. However, it will not ask the user sitting in front of the browser for it; there’s just no way that this can happen. The Python script is executed on your server, by php, and the user just waits for the php script to return some response (HTML) that is then displayed.

If you want to pass information to the Python script, you should pass data from php via command line arguments or the standard input, and then read the script’s output to present it back to the user.

But it will be a lot cleaner, if you just stick to a single language there: Either put the logic into the php script, that way you can interact easily with the user via the webserver, or replace the whole php script by a Python web stack that does the same.

poke
  • 369,085
  • 72
  • 557
  • 602
  • Thanks! The input portion of my code is not really necessary, so I can actually just remove it completely. In that case, would I still have issues running the script from the html button. Would I format the `php` differently? – Alina DW May 27 '15 at 19:56
  • You still can’t run server side code from the client’s browser and expect it to be interactive. HTTP works by sending a request to the server, and the server responds. Every interactivity will have to involve multiple requests, which you can’t just do by `exec`ing some script. – poke May 27 '15 at 19:58
  • I see. What I meant is if I take out the interactive portion of my python script, where it ran without promoting the user for any input, it should run smoothly right? I've done that to the script now, but the button still does not run the script. – Alina DW May 27 '15 at 20:01
  • Yes, if the HTML form launches the php file that contains the `exec`, and *if* running the Python script works from within php (permissions might be an issue; your server logs may tell you about that), then the script should run. You can try opening the php file directly in the browser, so you can skip the button-pressing part for test purposes. – poke May 27 '15 at 20:04