0

I have a python script (on Windows Server) that requests information through an API, downloads data into several csv and at the end it 'zips' them all into 1 single .zip file. The idea is to access that zip file through an URL, using PHP.

But the main issue here is PHP making the zip available, even before the file is not yet completely compressed.

Using the 'sleep' function, I believe will halt the execution of PHP, even the Python script, am I right?

My current setting is using a Task, on Windows Task Scheduler, to trigger the Python script, download the necessary files and only then, making the zip file available with PHP.

Is there any way of doing all the process within PHP script, without using Windows Task Scheduler?

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/#classes/simple_html_dom.php";

#header("Content-type: text/html");
header("Content-Disposition: attachment; filename=api-week-values.zip");
    
unlink("api-week-values.zip");
$command = escapeshellcmd('python api_weekly.py');
$output = shell_exec($command);

sleep(15);
?>

You can see the code I am using for the whole process.

To clarify the process:

  1. An application needs to access the file I am downloading with the python script
  2. I feed the application with the URL from Apache server
  3. the application goes to the link (through index.php) and triggers the python script (which downloads the zip file)
  4. php should pop-up a save file WHEN the zip file has been completely downloaded

2 Answers2

0

May be you can do something like that :

  • The python script put at the begining a file somewhere with informations like start time and end time when it endded
  • The PHP script loop on read the file while the end time is not set. And after delete the file. And then, it takes the zip.
svgta
  • 343
  • 1
  • 6
  • It is a good idea, however, the link is to be used several times, when needed and without any specific time. Accessing the URL is the triggering point of the Python script. – Joao Cerca Feb 11 '22 at 08:03
0

Why not call the PHP from Python at the end of the py script? Link

import subprocess

# if the script don't need output.
subprocess.call("php /path/to/your/script.php")

# if you want output
proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
womblerone
  • 552
  • 7
  • 18
  • I am sure if this is a solution for my problem. The process is: An application needs to access the file I am downloading with the script -> I feed the application with the URL from Apache server -> goes to the link -> gets the file (in this case api-week.zip). But for the zip file to be available, the php needs to let the python script to finish completely. – Joao Cerca Feb 11 '22 at 11:29
  • By calling php from python, the python code is blocking the following lines. Say you do your downloading on line 20, and then on line 21 you start processing the result. Line 21 will not be called until line 20 is done. It's quite hard to make script1 know the status of script2, much easier to integrate them into one script. – womblerone Feb 11 '22 at 13:17