[英]Run python script from the web in a Raspberry Pi web server
I have a python script that picks an URL as parameter and it fetch some data (normally it takes 30 seconds to finish) and creates a file with it. 我有一个python脚本,它选择一个URL作为参数,并获取一些数据(通常需要30秒才能完成)并使用它创建文件。 What I want is to be able to call the script from a web (I thought about doing it with PHP but i don't mind) and just get the file path (which is printed at the begining of the script process) and leave the script running in background. 我想要的是能够从网络上调用脚本(我考虑过用PHP来执行此操作,但我不介意),而只是获取文件路径(在脚本过程开始时打印出来),然后离开脚本在后台运行。
how can i do this? 我怎样才能做到这一点? which is the best way? 最好的方法是什么?
Note: I'm using a raspberry pi as a web server and the python file is located in /var/www/ 注意:我使用树莓派作为Web服务器,而python文件位于/ var / www /
php can execute system commands using the command system()
. php可以使用命令system()
执行系统命令。
To execute a python script, you can use this command: 要执行python脚本,可以使用以下命令:
system('/path/to/python /path/to/test.py');
To leave it running in the background, use &
: 要使其在后台运行,请使用&
:
system('/path/to/python /path/to/test.py &');
If you want to start the script in the background but still get the first lines of output, I would redirect the output to a file: 如果您想在后台启动脚本,但仍获得输出的第一行,则可以将输出重定向到文件:
system('/path/to/python /path/to/test.py >/var/www/unique_file.txt &');
sleep(500); // Script has printed the file path now
$output = file_get_contents("unique_file.txt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.