在网页中从PHP调用Python脚本时没有完全执行?

2024-04-23 07:10:41 发布

您现在位置:Python中文网/ 问答频道 /正文

Python脚本在从PHP调用时只执行前几行代码,而在从命令行执行时执行得非常完美。
我在windows上通过xampp服务器运行PHP。我对此做了很多研究,但没有找到任何有用的方法来解决我的问题。我在这里上班,因为我必须在早上把这个交给我的老板,任何帮助都将不胜感激。
下面是我用来从PHP执行python脚本的代码片段

$cmd = 'python C:\xampp\htdocs\aws_engine\images\aws_engine_script.py';
    $command = escapeshellcmd($cmd);
        //$run =  shell_exec($command);
        $pid = popen( $command,"r");
        while( !feof( $pid ) )
        {
            echo fread($pid, 256);
           flush();
           ob_flush();
           usleep(100000);
        }
        pclose($pid);  

下面是我的python脚本,它与AWS S3建立连接,并将图像文件从目录上传到S3 bucket,但是,我的脚本在第二个print语句之后停止:

#header files
import os  
import tinys3
import glob
from subprocess import check_output
from time import gmtime, strftime

#Providing AWS Access and Secret key credentials
print('Establishing connection to S3..')
conn = tinys3.Connection('XXXXXXXXXX','XXXXXXXXXXX',tls=True)

print('Connection established & uploading files...')

#loop for uploading image files with in a local directory of extensions PNG, JPG & JPEG 
for root, dirs, filenames in os.walk('C:/xampp/htdocs/aws_engine/images/'):
    for f in filenames:
        if f.endswith(".jpg") or f.endswith(".jpeg"):
            k = open(f,'rb')
            conn.upload(f,k,'deeplearning-test-bucketsppu')
            print (f)

print('\n All files Uploaded successfully !')

Tags: 代码inimport脚本cmdawsfors3