I am building simple web kids programming learning page in PHP. Application will test python scripts for correctness. I am not very strong in Linux. Server OS is Centos.
我正在用PHP构建简单的web孩子编程学习页面。应用程序将测试python脚本的正确性。我在Linux上不是很强大。服务器操作系统是Centos。
1)So, how can I run python script from PHP, so that it would be safe for server? What are main things that I should take into account? And also, PHP should be able to stop execution of script, if time limit is reached.
1)那么,如何从PHP运行python脚本,以便它对服务器安全?我应该考虑哪些主要内容?而且,如果达到时间限制,PHP应该能够停止执行脚本。
2)Python script will get input with raw_input() and will give output with print(). How can I set input for python scripts and get output from scripts with PHP?
2)Python脚本将使用raw_input()获得输入,并将使用print()提供输出。如何设置python脚本的输入并使用PHP从脚本输出?
3 个解决方案
#1
0
First question: Nothing is 100% safe, but you can take some cares:
第一个问题:没有什么是100%安全的,但你可以采取一些措施:
- put your python scripts not below
/public_html
(or/www
), but in some hidden directory; - set them permissions to
0700
(orrwx------
), so only you can read, write and execute them (assuming that your user is the user which runs the PHP scripts). If you have access to terminal, you can do it by doingchmod 0700 script.py
.
把你的python脚本放在/ public_html(或/ www)下面,但是在一些隐藏目录中;
将它们的权限设置为0700(或rwx ------),因此只有您可以读取,写入和执行它们(假设您的用户是运行PHP脚本的用户)。如果您有权访问终端,可以通过执行chmod 0700 script.py来完成。
Second question: you can use proc_open to execute your scripts and to exchange information with them.
第二个问题:您可以使用proc_open来执行脚本并与它们交换信息。
#2
0
Try This Code :
'$command = escapeshellcmd('YOUR_FILE.py');
$output = shell_exec($command);
echo $output;'
#3
-3
The following should be very safe for your server
以下应该对您的服务器非常安全
<?php
echo "<a href='http://www.codepad.org'>Click here, click python, enter your code, and click submit</a>";
?>
#1
0
First question: Nothing is 100% safe, but you can take some cares:
第一个问题:没有什么是100%安全的,但你可以采取一些措施:
- put your python scripts not below
/public_html
(or/www
), but in some hidden directory; - set them permissions to
0700
(orrwx------
), so only you can read, write and execute them (assuming that your user is the user which runs the PHP scripts). If you have access to terminal, you can do it by doingchmod 0700 script.py
.
把你的python脚本放在/ public_html(或/ www)下面,但是在一些隐藏目录中;
将它们的权限设置为0700(或rwx ------),因此只有您可以读取,写入和执行它们(假设您的用户是运行PHP脚本的用户)。如果您有权访问终端,可以通过执行chmod 0700 script.py来完成。
Second question: you can use proc_open to execute your scripts and to exchange information with them.
第二个问题:您可以使用proc_open来执行脚本并与它们交换信息。
#2
0
Try This Code :
'$command = escapeshellcmd('YOUR_FILE.py');
$output = shell_exec($command);
echo $output;'
#3
-3
The following should be very safe for your server
以下应该对您的服务器非常安全
<?php
echo "<a href='http://www.codepad.org'>Click here, click python, enter your code, and click submit</a>";
?>