Jump to content

run python script from php page


Lassie

Recommended Posts

I need to execute a python program called from inside a php web page.

the web page runs without errors and the python script will run from a terminal but the two together dont dance.

Any help would be welcome.

php

<?php
/* 
 * Template Name:message
 * 
 * 
 */
?>
<?php get_header(); ?>

		<div id="container">
			<div id="content" role="main">
                <?php
			/* Run the loop to output the page.
			 * If you want to overload this in a child theme then include a file
			 * called loop-page.php and that will be used instead.
			 */
			get_template_part( 'loop', 'page' );
			
			//get  message
			//print_r($_GET);
			//echo "<br />";

			//$message= $_GET['message']; Will be appened to $command
			$command= "/home/pi/sense-test.py";
			exec($command);
			
			echo $message;
			echo "<br />";
			echo "Message Recieved";
			echo "The time is" . date9"h:i:sa");
?>
                        </div>
                </div>
                
                
                

<?php get_footer(); ?>


Python

#!/usr/bin/env python3

from sense_hat import SenseHat

sense = SenseHat()

sense.show_message("Hello World")
Link to comment
Share on other sites

Let's clarify things a bit.  There are no PHP webpages.  Web pages are basically HTML.  So is your question really "how to run a python script from a PHP script"?  And did you try googling THAT question?  I did and found several responses.

Link to comment
Share on other sites

Yes you are right it is how to run the python script from a php script that happens to be a wordpress web pgae running on an apache server.

Yes I have tried googling and my latest effort is 

$command = escapeshellcmd('/home/pi/sense-test.py');
$output = shell_exec($command);
echo $output;

I get no output.

I have checked permissions to make it executable.

My thinking now is to move the file to the Apache folder and work from there.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.