Call Python script from php with exec() - php

I have one python script for handling outlook emails using Outlook MAPI. I have to call this script from PHP code. If I call script from cmd it is fine, but when I do it from PHP doesn't work and there are no errors. Also if I just call some other python script with simple file writing code from same PHP script it works. I have no idea what to do. I am trying with exec('python c:/xampp/SCRIPT_LOCATION/SCRIPT.py') as well as shell_exec(), system()... but with no progression.

Try this:
$python = 'C:\\Python27\\python.exe';
$pyscript = 'C:\\xampp\\SCRIPT_LOCATION\\SCRIPT.py';
exec("$python $pyscript");

Related

Run python script from php on webserver

I have a free webspace hosted by altervista where i have one script python and one script php. What i want to do is call the python script using the php script. I googled for a solution and i found several questions about it, i tried different proposed solutions but no one worked for me.
Here the php script code:
<?php
$command = escapeshellcmd('test.py');
$output = shell_exec($command);
echo $output;
?>
and here the python script code:
print("Hello")
When i run the php script nothing happens.
What's wrong?
Thanks
Your hosting company might have disabled the shell-exec function for security reasons. I'm not saying it is the case, but you should check that first before even trying to run shell_exec.
Seealso:
PHP - How to know if server allows shell_exec
Plus, back in 2012, it wasn't possible with Altervista...

including python code in php

I want to include the following python code into php. The name of the python file is hello.py. The contents are
print "Hello World"
I want to call this python script in php and show the same in webpage
Not really possible like that. What you could do is use exec or passthru to run the python in the terminal and return the results. Best option would be to do a separate call to the python script and combine them on the client.

Error on calling python script from php

Well, I have to call a python script from a php one but it does nothing. I've been searching but i couldn't fix it. The script creates 2 files and writes things in the terminal.
Here's my code:
exec('/usr/local/bin/python /usr/local/script/myScript.py '.$path.' '.$specie.' '.$pvalue.');
Thank you very much.

To call a perl script as an external program in a PHP script

I want to call a perl script as an external program in a PHP script, once the PHP script finished its run, it should send back the output to a html page. I tried using exec command to call the perl script but it dint work out. please help with this simple perl script, so that i can try using the same !
Perl script:
#!/usr/bin/perl -s
$var1 = 'high';
print $var1;
Thanks ahead of time !!
the output of commands called via exec will be stored in the output parameter that you give (hence why it's specified as by reference) either print out the contents of that array or use the passthru() function instead
references:
http://www.php.net/manual/en/function.exec.php
http://www.php.net/manual/en/function.passthru.php
so just
passthru('/usr/bin/perl yourscript.pl');
instead of
exec('/usr/bin/perl yourscript.pl');
I would suggest trying
exec ('path_to_your_script')
This will work for any shell executable or command call, whenever you want to execute it from your PHP script.

Running a Python script in the background from a PHP file

I need to run a Python script in the background after being called from a PHP file. The PHP file should continue to run independently of the Python script (i.e. it shouldn't hang waiting for the Python script to finish processing, but should instead carry on processing itself).
The Python script takes one argument and produces no output (it merely processes some data in the background), then exits. I'm running Python 2.6, PHP 5.2.6, and Ubuntu 9.04.
You could use exec() to kick off the Python interperator and have it send its output to either a file or to /dev/null with redirection. Using the & operator in the exec call will cause the command to be started and PHP to continue without waiting for a result.
http://www.developertutorials.com/tutorials/php/running-background-processes-in-php-349/ goes into more detail.
PHP Process Control can be used for this. The proc_open command can be used to start a process. You can later check up on it, read it's output etc.
View the manual entry: http://www.php.net/manual/en/function.proc-open.php and search around google for PHP Process Control
I'm guessing the PHP file is called via Apache, in which case you won't be able to fork(). You should make your Python script daemonize. Check out python-daemon.
You could use:
<?php
shell_exec('./test.sh &');
?>
where ./test.sh should be the execution line to your script

Categories