I want to run python script from php.
this is my python code. It is saved in /home/pi and name of file is hello.py
#! /usr/bin/python
import bluetooth
bd_addr="xx:xx:xx:xx:xx:xx"
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr.port))
data=""
while 1:
try:
data +=sock.recv(1024)
data_end=data.find('\n')
if data_end!=-1:
rec=data[:data_end]
print datas
data=data[data_end+1:]
except KeyboardInterrupt:
break
And here is my php code. It is saved in /var/www/html and name of file is php.php
<?php
$output=shell_exec('ls -l /home/pi/hello.py');
echo "<pre>$output</pre>";
?>
And I insert localhost/php.php in chrome, it displays
-rw-r-r- 1 pi pi 378 Mar 8 12:07 /home/pi/hello.py
what is the problem??
As pointed out by Jon Stirling, you are using "ls" to only listing the content of the folder or to check whether the file exist in that folder. To run the Python code, you need to change the PHP file into something like this:
<?PHP
$output=shell_exec('./hello.py');
echo "<pre>$output</pre>";
?>
ls command is used to list files in a directory or to get information about a file. You are ls-ing on your python file and the result is correct. It is providing you with information about the file.
Just put the file name inside of shell_exec that is /home/pi/hello.py. If you do not want to depend on the shebang and the command python is available in your shell environment then you can use python /home/pi/hello.py instead of bare /home/pi/hello.py.
Again, you used the variable datas with print where you intended to use data - fix it.
php code:
<?php
$output=shell_exec('python /home/pi/hello.py');
echo "<pre>$output</pre>";
?>
or:
<?php
$output=shell_exec('/home/pi/hello.py');
echo "<pre>$output</pre>";
?>
python code:
#! /usr/bin/python
import bluetooth
bd_addr="xx:xx:xx:xx:xx:xx"
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr.port))
data=""
while 1:
try:
data +=sock.recv(1024)
data_end=data.find('\n')
if data_end!=-1:
rec=data[:data_end]
print data
data=data[data_end+1:]
except KeyboardInterrupt:
break
Related
I've got IIS 8.5 installed, with PHP installed and running. I've also added a handler mapping (with the %s %s at the end of the python.exe path) to the website. I can run the python script directly from the browser successfully (prints "Hello World").
This is all just configured on the Default Web Site for testing purposes. When I attempt to set a PHP variable to the output of a python script, the variable does not appear to receive the value. IUSR and IIS_IUSRS have modify permissions on the root directory (C:\inetpub\wwwroot), which is where all HTML, PHP and Python files are located.
My code is as follows:
PHP:
<?php
$coutput = shell_exec("/test.py > ~debug.log 2>&1");
include 'header.html';
echo "</br>Python Output:</br>" . $coutput;
var_dump($coutput);
include 'footer.html';
?>
NOTE: I've also tried "python test.py > ~debug.log 2>&1" in the shell_exec function to no avail.
Python (blank print is to display properly in browser when run directly):
print("")
print("Hello World")
Initially, I thought this was a permissions issue with PHP, so I ran the python script from PHP via command line and get the python code:
c:\inetpub\wwwroot>"c:\program files\php\v7.3\php.exe" c:\inetpub\wwwroot\test.py
print("")
print("Hello World")
PHP is not in safe mode.
What am I missing? Any help would be appreciated!
I want to run bat file via php.This bat file contains power shell script.I am able to run this via cmd but not via php.
AwsExecute.bat is,
powershell -command ./AWSExecuter.ps1
This AwsExecuter.ps1 which is inside the C:\AWS\Distributed-setup-3 folder.
In php I have tried like this,
Trail.php which is in http://localhost/performance folder.
<?php
exec('C:\AWS\Distributed-setup-3\AwsExecute.bat');
echo "Done";
?>
As well as,
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\AWS\Distributed-setup-3\AwsExecute.bat');
echo "Done";
?>
Both are not working here, I tried to another bat file which just creates a php file it works fine.
That is First.bat,
echo off
break>"C:\Users\Vidya\Desktop\performance.php"
echo done
Try running system
system("cmd /c C:\AWS\Distributed-setup-3\AwsExecute.bat");
For more on system read this
I've moved my code form one WAMP computer to another and the code that runs pdftk stopped working. I've compared the permissions on pdftk.exe and they are the same on both machines. When I run the same command from a command prompt, it works. I add exec("whoami") to the script and the user is the same on both computers. When I run something like exec('dir 2>&1', $out) it executes so I know exec is working form within php.
I've created a trivial php file to test and it doesn't work.
<?php
$String = 'pdftk.exe > "c:\temp\temp.txt"';
exec("$String");
exec("pdftk.exe > \"c:\temp\temp.txt\"");
?>
Both exec command result in a 0 byte file being created.
if I run
pdftk.exe > "c:\temp\temp.txt"
from a command line it puts the output of pdftk.exe into the temp.txt file as expected.
This seems like is must be some kind of permissions issue, but permissions on the executable seem to be the same. Losing my mind on this.
In my opinion, first line should be:
<?php
$String = 'pdftk.exe > "c:\temp\temp.txt"';
exec($String); //removed quotes
?>
And for the second line, what if you try this?
<?php
exec('pdftk.exe > "c:\temp\temp.txt\"', $outputAndErrors, $returnValue);
var_dump($outputAndErrors);
?>
Or if you remove your first ">"?
<?php
exec('pdftk.exe "c:\temp\temp.txt\" 2>&1', $outputAndErrors, $returnValue);
var_dump($outputAndErrors);
?>
These are some tests that may help you, thought.
I'm working on classification algorithm in python and it related with php code, to get the right result I should run my python exe then go to the php code.
I want to execute the python by itself with php code. I tried this:
<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');
?>
But It's not working, should I know more about path? and how to put my exe in the write path?
<?php
exec("C:\\Users\\posh\\AppData\\Local\\Programs\\Python\\Python35\\python.exe YOUR_PYTHONSCRIPT.py <arguments if any>", $output, $ret_code);
// directly writing python may not work in that case
// give path to python.exe
exec("python YOUR_PYTHONSCRIPT.py <arguments if any>", $output, $ret_code);
// $ret_code : returns the code 0 or 1
// output is an array
?>
I expected this to be quite simple but I cannot make it work.
I'm trying to run a php script in command line (to finally be able to automate it in a cron.)
The file is outside of the public web folder to avoid executing it from a webbrowser.
File is called 'daily.php' and contains :
#!/usr/bin/php
<?
echo "hello \n";
file_put_contents("daily.log", "executed...", FILE_APPEND | LOCK_EX);
?>
then in my terminal I run (as root):
/usr/bin/php daily.php
but it simple outputs me the full source code without the hashbang.
So I tried changing the file to 755, tried to chmod +x it but still outputs me the full source code.
I had a look in the man page and I found :
-f <file> Parse and execute <file>.
Tried this but still the same output.
Why is this? how can I interpret this file?
Ok
I found the answer after 2 hours of trouble, and just after posting this question.
Even if you hashbang the file, even if you call php directly, you need to implicitly tell it is php by using :
<?php
?>
and not
<?
?>