Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

Run python script and passing variables using PHP

Hi am having trouble passing a variable from PHP to python. It is not displaying on the webpage

PHP

<?php 
  $item='example';
  exec("python pytest.py $item");
?>

Python

import sys
print(sys.argv[1])

I am running WAMP and both the PHP and Python file is in the same working directory. However, nothing is displayed on the PHP webpage. Please help, thank you.

question from:https://stackoverflow.com/questions/66059288/run-python-script-and-passing-variables-using-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think the only thing you are missing in your PHP script is echo.

If you call exec("python pytest.py $item"); PHP won't just print the output of the called command.

You need to echo shell_exec("python pytest.py $item");.

The difference between exec and shell_exec is that shell_exec will return the output of the command but exec won't. You can read more about it here: https://www.php.net/manual/en/function.shell-exec.php and https://www.php.net/manual/en/function.exec.php.

Also make sure the user, which is running the PHP-File, has permissions on the pytest.py file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...