Python脚本在从PHP启动时不会启动另一个Python脚本

2024-04-26 07:19:06 发布

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

我已经编写了下面的测试php代码,它将运行python脚本,注意这里列出了3个方法(都可以)。最后一个方法将运行python脚本,但不会导致PHP页面等待它完成。你知道吗

 <?PHP
  $SKU= 'asdfghyt42';
  $showID = '60';


  //method 1
  $command='C:\Python27\python.exe python\pythontesting.py $SKU $showID';
  $buffer='';
  ob_start(); // prevent outputting till you are done
   passthru($command);
  //get the result out
 $sketchfabKey=ob_get_contents();
  //clean up the buffer
 ob_end_clean();
 echo $sketchfabKey; 


 /*
 /method 2
 $command = escapeshellcmd('C:\Python27\pythonw.exe python\pythontesting.py $SKU $showID');
$output = shell_exec($command);
echo $output;


 // method 3
$command="C:\Python27\pythonw.exe python\pythontesting.py $sketchfabKey $SKU"; 
$out = 0;
ob_end_clean();
ignore_user_abort();
ob_start();
header("Connection: close");
echo json_encode($out);
header("Content-Length: " . ob_get_length());
ob_end_flush();
flush();
  exec($command);
  */

 ?>

python脚本将打开一个windows消息框,告诉您它是python脚本1

import win32api
import sys
import subprocess

win32api.MessageBox(0, '1:', 'yay', 0x00001000)
p = subprocess.call([sys.executable, 'pythontesting2.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print "I am alive"

一旦用户单击消息框上的ok按钮,它应该运行下一个python脚本,该脚本对声明为script2的消息框执行相同的操作

import win32api
import sys
import subprocess

win32api.MessageBox(0, '2:', 'yay', 0x00001000)
p = subprocess.call([sys.executable, 'pythontesting3.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print "I am alive"

这就是问题所在。如果我从pythonshell运行第一个python脚本,它将完全按照预期工作,并给我一个带有1的messagebox和一个带有2的messagebox。你知道吗

但是,当PHP页面执行第一个python脚本时,python将只执行第一个脚本中的代码,并给我一个带有1的messagebox。当我单击ok时,我没有得到第二个消息框,因此得出结论,第二个python脚本从未启动。你知道吗

我想这可能是windows的权限问题,设置python.exe以管理员身份运行,并确保所有权限都设置为完全控制。它没有改变任何事情。

以下是一些关于系统的信息,如果这有帮助的话:
Python 2.7版
webserver通过Apache在本地运行,并使用XAMPP进行安装


Tags: pyimport脚本消息sysexemethodcommand