Report this

What is the reason for this report?

How to execute script pyhon with php?

Posted on October 13, 2017

I have a project PHP in my droplet LEMP 16.04 with script python, when I run in terminal is ok, but when I run in the browser don’t execute the script.

My PHP

$vehicleJSON = shell_exec('python vehiclepy.py ' . $plate);

I’ve already tried:

  1. change to $vehicleJSON = shell_exec(‘python vehiclepy.py ’ . $plate . ’ 2>&1’);
  2. change to $vehicleJSON = shell_exec('usr/bin/python /var/www/myapp/vehiclepy.py ’ . $plate);
  3. add in sudoers www-data NOPASSWD: /var/www/myapp but don’t execute.

Please help me kkkkk, thanks!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I did it! I change the function to system().

First off, be careful running shell_exec like this, If the user can change ** $plate** they would be free to run any command they like.

You may find if you can’t execute it when running via HTTP it may be a blocked function.

https://stackoverflow.com/questions/24999673/how-to-enable-shell-exec-and-exec-on-php

Do you have anything in your error logs?

Heya,

Executing a Python script via PHP can be achieved using functions like exec(), shell_exec(), or system(). However, it’s important to note that there are potential security risks when running scripts this way, especially if the script or parameters are influenced by user input. Always validate and sanitize user input and limit the potential operations the Python script can perform.

Here’s a basic example of how to execute a Python script using PHP:

  1. Let’s say you have a Python script named script.py:
# script.py
print("Hello from Python!")

You can execute this Python script from a PHP file using the shell_exec() function:

<?php
$output = shell_exec('python3 /path/to/script.py');
echo $output;
?>

Here’s a brief overview of the PHP functions you can use:

  • exec(): Executes a command, and returns the last line of the output.
  • shell_exec(): Executes a command via the shell and returns the complete output as a string.
  • system(): Executes a command and directly outputs the result.

When using these functions:

  1. Ensure the PHP user (often www-data on Linux) has permissions to execute the Python script.
  2. Use the absolute path to both the Python interpreter and the script, especially if you’re running this in a web server context. For example: /usr/bin/python3 /absolute/path/to/script.py.
  3. Always be careful with user input. If any part of the command relies on user input, even indirectly, you run the risk of a command injection attack. Sanitize and validate all input.

Remember to enable the necessary permissions and ensure that the path to the Python interpreter is correctly specified. If PHP is in safe mode or functions like exec() are disabled (which is common in many shared hosting environments), this method will not work.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.