tags:

views:

78

answers:

4

Hi, Can I simply execute Python program using PHP like this? (in a browser)

exec("python myProgram /Applications/MAMP/htdocs/somefile.xml");

or like this:

exec("/path/to/python path/to/myProgram /Applications/MAMP/htdocs/somefile.xml");

Is any of this method correct?

If not, what should be the right way to do it?

Thanks

A: 

Yeah, why not?

Note that you are not executing this "in the browser" but rather on the server side, while the page is being pre-processed.

Consider that the page will not return until the exec has finished, so it really depends on what exactly you are trying to do.

Yuval A
basically I have a python program that I am able to run successfully like this in terminal (on mac): >>> myptogram myfile.xml ..... Now I am trying to achieve the same using PHP. If I do the way I showed in my post, will the result be same as my terminal window approach?
ssdesign
check this out tooo.... intresting http://www.php.net/manual/en/language.operators.execution.php
Jaison Justus
@ssdesign Yes, but why can't you just rewrite your python program in PHP? Because relying on exec() means you'll need to set up python wherever this code is going to run AND ensure you are allowed to use exec()
quantumSoup
I have faced situations working on an environment with legacy code written in some other languages and I wish to execute them - than rewriting them in PHP.Programmers are lazy ;)
yclian
A: 

check this link http://forums.powweb.com/showthread.php?t=77376

check this out tooo.... intresting http://www.php.net/manual/en/language.operators.execution.php

Jaison Justus
It would be helpful to provide some content and your opinions than just links.
yclian
first thing i am sorry for posting links..i think links are more help full.i know links will never answer what you are looking but give you some extra knowledge sometime.... its my experience... sorry... iam reallly sorry...have a nice day
Jaison Justus
@Jaison Links are helpful, but it's also helpful to provide a summary here of what the links say
Michael Mrozek
sure from next time i will do michael
Jaison Justus
+1  A: 

if you want to capture output as well, use proc_open (full fd connectivity, i.e. input and output) or popen (either in- or output)

mvds
A: 

I would prefer using proc_open() as suggested by mvds as you can't write to STDIN nor read from STDOUT with exec()/shell_exec(), as well as providing your own set of environment variable -$_ENV.

A sample snippet extracted from my code:

$process = proc_open(
    "{$command}",
    array(            
     array('pipe', 'r'),
     array('pipe', 'w'),
     array('pipe', 'w')
    ),
    $pipes,
    NULL,
    $_ENV
);

if(is_resource($process)){

    fwrite($pipes[0], $string);
    fclose($pipes[0]);

    $rt = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    $rtErr = stream_get_contents($pipes[2]);
    fclose($pipes[2]);

    $exitCode = proc_close($process);

}

Read more: http://php.net/manual/en/function.proc-open.php

yclian