I am running a PHP web script locally for testing purposes through terminal with command: php -S localhost:8000
.
<?php $command = '/usr/bin/python /Users/Jupiter/Desktop/NC/www/createHarbourContainter.py'; exec($command); ?>
I am trying to call this python script that exists in the same directory:
#!/usr/bin/python
import os
import sys
save_path = '/Users/Jupiter/Desktop/NC/harbours/'
name = sys.argv[1]
def newHarbourContainer():
os.makedirs(save_path + name)
def main():
newHarbourContainer()
if __name__ == '__main__':
main()
This python script has chmod x+
set to it.
I am able to run the python script from the terminal : python createHarbourContainter.py
What I am unable to do is get the PHP function exec()
to invoke the python script. Help needed!
In PHP you can use backticks ` to execute shell commands, so try this:
<?php
`/usr/bin/python /Users/Jupiter/Desktop/NC/www/createHarbourContainter.py`
?>
See http://php.net/manual/en/language.operators.execution.php for more info
So I have found a solution:
$output = shell_exec('python createHarbourContainer.py')
And to get back an output into the browser:
echo "<pre>$output</pre>";
I was running the server in PHP Development Server
. Using bash command: php -S localhost:8000
from directory where index.php is located. The server was logging all input and output from browser interface. I realized that when calling: /usr/bin/python
from php the PHP Development Server
would halt and open python interpreter.
And setting the whole path to the python script I wanted executed didn't work because the PHP script didn't have permission.
How this can help someone else in future.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.