0

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/

  • Use a web framework such as bottle or flask. They are very light and quick to develop something simple like this. – Paul Rooney May 03 '15 at 12:49
  • go Here browse the website you will find what you are looking for http://hackaholic.info/category/raspberry-pi/ – Hackaholic May 03 '15 at 13:06
  • you could do you own "webserver" like that return a script... http://stackoverflow.com/questions/16640054/minimal-web-server-using-netcat/21204114#21204114 – maco1717 Aug 02 '16 at 14:44

1 Answers1

0

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");
ByteHamster
  • 4,884
  • 9
  • 38
  • 53