I'm new to php and html (internet languages in general) and I would like to execute a python script that takes a picture from my php page.
I did some research, edited some code (according to my understanding) and came up with this, but it doens't do anything.
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="" method="post">
<button type="submit" name="sub" value="call">Click</button>
</form>
</body>
</html>
<?php
if(isset($_POST['sub']))
{
exec('sudo python take_picture.py');
echo "Picture captured";
}
?>
I managed to execute the python script from a html page invoking a simple php script separately, but when it takes the picture it sends me to a new page (blank page with the text displayed) which I don't want. Here are the codes:
HTML button:
<form action="take_picture.php" method="post">
<button>Click</button>
</form>
PHP script:
<?php
exec('sudo python take_picture.py');
?>
What I need is to simply press the "click" button and take the picture without sending me to anywhere else.
Could you please guys aid me to achieve what I need and explain with apples what I was doing wrong or what I missed.
Thank you in advance!
You can make an :
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
More information on this Stack Overflow Question.
Running a Python script from PHP
Related
I have a python file which generates 10 blank files. I have another php file with html button. When the button is pressed the python script should run.
When I run python file on terminal, it works fine and generates blank files.
But when i click button on php, it does nothing.
Can anyone help me out with this problem?
The php and python files are attached below.
upload.py:
for i in range(0,10):
f=open(str(i)+"upload.txt", "a")
f.write("This is line")
f.close()
upload.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post">
<input type="submit" value="GO" name="GO">
</form>
</body>
</html>
<?php
if(isset($_POST['GO']))
{
shell_exec("python upload.py");
echo"success";
}
?>
I have an account on Gator.
I am trying to run a php script as follows
;$command = escapeshellcmd('./simple.py 2>&1');
$output = shell_exec($command);
echo $output
I would like to get as output anything that the python script prints to screen, but the output is empty no matter what I try (I use standard print in python
Use my method as a reference.
I have this two files
run.php
mkdir.py
Here, I've created a html page which contains GO button. Whenever you press this button a new folder will be created in directory whose path you have mentioned.
run.php
<html>
<body>
<head>
<title>
run
</title>
</head>
<form method="post">
<input type="submit" value="GO" name="GO">
</form>
</body>
</html>
<?php
if(isset($_POST['GO']))
{
shell_exec("python /var/www/html/lab/mkdir.py");
echo"success";
}
?>
mkdir.py
#!/usr/bin/env python
import os
os.makedirs("thisfolder");
$command = escapeshellcmd('./simple.py 2>&1');
escapeshellcmd is a form of surrender, it's php stupidity, it gets used as a DWIM band-aid when the programmer has lost control of the command-line.
It's breaking your stderr redirection.
Use escapeshellarg where you need to escape shell stuff.
$command = escapeshellarg('./simple.py') . ' ' . '2>&1';
My html page has a button and when I click that button I need to execute a python file. I don't need to show the python file content in browser, I only need to execute the python file by clicking the html button. When executing the python file, it will generate a json file which I need.
This is my code and it's opening the python file but not showing any output.
<html>
<body>
<h1> DEVELOPMENT </h1>
<form action="phpfile.php" method="post">
<input type="submit" name="someAction" value="GO" />
</form>
</body>
</html>
<?php
$result_last_line = system("python C:/xampp/htdocs/NEW/pythonfile/identifyrooms.py");
header('Location: view.html');
exit;
?>
When I run my python file using editor the output console is as below.
Can anyone find the error?.
Thanks
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!
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;
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.
I have a webserver in Android using KSWEB application. It can support php, mysql, etc. I also have installed SL4A and python interpreter on my Android. I want to execute a python script (that runs on server side, the Android) on the click of a button.
All I have for now, is this:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<button name=test>TEST</button>
</body>
<?php
if(isset($_POST['test']))
{
exec('python test_andrei.py');
echo ('Worked!');
}
?>
</html>
Of course nothing happens when I click the button (both on server side (no script executed) and on client side (no echo output))... I am a complete noob in php and python...
Thank you for your help!
First of all wrap the name value in quotes for the button:
<button name="test">TEST</button>
Secondly you need to wrap the button (and any other inputs) in a form for that isset($_POST['test']) to be submitted as expected:
<form method="post" action="<?php echo html_entities($_SERVER['PHP_SELF']); ?>">
<button type="submit" name="test">TEST</button>
</form>
and also as you can see i added the action to the form so it submits to the same page