i want to execute a python script on my server which execute and return a list of data.
How can i execute python script by php and return back the python list data in php code.
for example.
python_file.py
import os
def list_data():
"""
this function will return a content list of path directory
:return:
"""
path = 'C:/'
my_list = os.listdir(path)
return my_list
print list_data()
use this:
<?php
$data_list = `python path/to/python_file.py`;
?>
With exec command you can execute a shell command.
http://php.net/manual/en/function.exec.php
So basically any script ( including python ) can be executed.
But using exec is not encouraged in web apps. It basically lets you run shell commands and execute them on the server. Most shared hosts come with it disabled.
Related
I am making an android application in which I am first uploading the image to the server and on the server side, I want to execute a Python script from PHP. But I am not getting any output. When I access the Python script from the command prompt and run python TestCode.py it runs successfully and gives the desired output. I'm running Python script from PHP using the following command:
$result = exec('/usr/bin/python /var/www/html/Source/TestCode.py');
echo $result
However, if I run a simple Python program from PHP it works.
PHP has the permissions to access and execute the file.
Is there something which I am missing here?
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $output);
var_dump($output);
2nd Parameter of exec will give output
EDIT:
exec('/usr/bin/python /var/www/html/Source/TestCode.py 2>&1', $output);
2>&1 - redirecting stderr to stdout. Now in case of any error too, $output will be populated.
First Check your python PATH using "which python" command and check result is /usr/bin/python.
Check your "TestCode.py" if you have written #!/usr/bin/sh than replace it with #!/usr/bin/bash.
Than run these commands
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $result);
echo $result
I want to access the Terminal from a PHP script to open a standalone version of Maya and then run a Python script in Maya from the Terminal.
I've been able to send commands to the Terminal from my PHP script using the exec() function. For example, I've been able to open Maya with an .OBJ file using the following code.
//Open Maya with .OBJ
$cmd='open -a /Applications/Autodesk/maya2015/maya.app /Applications/AMPPS/www/webGL/upload/Character.obj';
exec($cmd);
I then found that I could open a Maya Python interpreter in the Terminal using this directory:
/Applications/Autodesk/maya2016/Maya.app/Contents/bin/mayapy
When this directory is executed in the Terminal, it runs the Maya Python interpreter. When the interpreter is running it seems that I am no longer able to send commands (which is now Python code) to the Terminal from my PHP script.
Am I going down the right path with using the exec() function to carry out what I want to do? Or does this function simply execute single commands and not multiple commands consecutively in the same Terminal? Do I need to use a different function that sends commands to a specific shell so that all the commands are being executed in the same Terminal?
Here is the code that I have been trying to send to the Terminal to run the Maya Python interpreter and then import the Maya standalone library and initialise it.
//Open Maya interpreter
$cmd2="/Applications/Autodesk/maya2016/Maya.app/Contents/bin/mayapy";
//Import standalone and initialise
$cmd3="import maya.standalone; maya.standalone.initialize( name='python' )"
//Execute commands
shell_exec($cmd2);
shell_exec($cmd3);
You don't need to code separate with mayapy, you can run script directly with mayapy
test.py
import os
import maya.standalone
maya.standalone.initialize()
import maya.cmds as cmds
print dir(cmds)
You can simply call this script like this
mayapy /your/path/test.py
In your case
$cmd2="/Applications/Autodesk/maya2016/Maya.app/Contents/bin/mayapy /your/path/test.py";
shell_exec($cmd2);
I am trying to run the command prompt using a php script on camps but I am unable to do it.
I could run the Explorer using
exec("explorer");
But when I try to run
exec("C:\Windows\system32\cmd.exe");
It doesn't execute
How do it do it?
I want to run a command like
ping Google.com
This reads commands from the standard input and executes them:
while (true) {
$command = readline("Command: ");
passthru($command);
}
Note that if you run a command like ping, you might not be able to stop it this way (usually you would hit Ctrl + C). However you can specify the number of pings to send:
ping -c 3 google.com
You can use the "exec()" function to run the code in background. The command prompt won't be displayed but will directly run the code.
Now if you had to run a python script from php:
You could use
exec("py "location of python script");
exec('py C:\xampp\htdocs\pro\helloworld.py');
The output would be in the directory where your php script is present.
i'm using an API, which creates a JSON file when the python script is executed, so i've tried to use exec to run the python script and thne retrieve the json. However the python script does not seem to execute. What am i doing wrong? i'm trying it on a apache using MAMP
exec('python http://localhost:8888/examples/recent_matches_to_json.py');
$json = file_get_contents('http://localhost:8888/examples/recent_matches.json');
$obj = json_decode($json);
var_dump($obj->recent, true);
Uh... You should download the script and save it, then run the interpreter on it.
Something like:
$py_script = file_get_contents('http://localhost:8888/examples/recent_matches_to_json.py');
file_put_contents('recent_matches_to_json.py', $py_script);
exec('python recent_matches_to_json.py');
If you intend to download the script from the local computer, why not run it directly? Like exec('python /home/peter/site/examples/recent_matches_to_json.py').
You could also set up a web/WSGI server in that Python script so you could run it directly by just sending a HTTP request (like http://localhost:9999/recent_matches_to_json/).
How I can execute those two command line via php:
wkhtmltopdf www.google.com gg.pdf
&
oofice -headless -nologo -pt cup-pdf my.doc
they both return a pdf file and download into my home directory.
I want to know the way to execute those command from my html page via php.
Thanks.
You should take a look at the System program execution section of the manual : PHP provides several functions that can be used to launch external commands / programs, including :
exec() -- which can store the output of the command in an array
shell_exec() -- which returns, as a string, the output of the command
system() -- which echoes the output of the command
To create a pdf from php(in linux) you must use a wrapper.
$cmd = '/usr/bin/xvfb-run --server-args="-screen 0, 1920x1080x24" /usr/bin/wkhtmltopdf http://google.com /tmp/google.pdf';
exec($cmd);