简体   繁体   中英

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. 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.

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/

php can execute system commands using the command system() .

To execute a python script, you can use this command:

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");

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM