1

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??

fjkdj
  • 11
  • 1
  • 1
  • 5

2 Answers2

1

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
Md. Sabuj Sarker
  • 669
  • 5
  • 12
1

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>";
?>
Kardi Teknomo
  • 1,375
  • 16
  • 24
  • thanks for answer sir. I follow your answer but web server displays 403-Forbidden what's wrong to me? – fjkdj Mar 09 '18 at 12:22
  • probably you have permission problem. You need to set chmod a+x filename.php – Kardi Teknomo Mar 10 '18 at 11:02
  • Is it permission problem? I made new php file and in web browser Hi is displayed, but output of python script is not displayed. – fjkdj Mar 10 '18 at 18:16
  • Try to run the python code using "./hello.py" on the server using ssh. If it works, then it should also work in PHP. If it does not work, try to use "python hello.py" – Kardi Teknomo Mar 12 '18 at 02:53
  • Would there be major robustness or scalability issues, in pursuing this practice for stuff that python can do but not PHP? – matanster Aug 26 '18 at 10:42