从PHP在Windows下运行python脚本(executing python script fr

2019-07-29 08:52发布

好吧,这可与我的Linux服务器,但我在家里我的电脑上测试我的网站,我试图让PHP执行我的python脚本,当然还有\ WINDOWS是唯一的路径改变时,我的Linux机器上的即时通讯。

$pyscript = 'C:\\wamp\\www\\testing\\scripts\\imageHandle.py';
$python = 'C:\\Python27\\python.exe';
$filePath = 'C:\\wamp\\www\\testing\\uploads\\thumbs\\10-05-2012-523.jpeg'


exec('$python $pyscript $filePath', $output, $return );

这个作品在我的Linux机器我的Windows测试服务器上,但没有。 我如何得到这个在Windows上运行? 哦,在窗口直接在命令提示符下运行时,这也适用

编辑:

这是作为你可以在答案阅读解决方案。

$cmd = "$python $pyscript $filePath";
exec("$cmd", $output);

奇迹般有效。 更改单蜱翻番。

Answer 1:

我觉得很奇怪,的文件路径C:\\wamp\\....会在Linux机器上运行。 无论如何,你试图不逃避斜线?

这个问题可以涉及到几个方面,包括实施不同的程序执行功能的OS的具体方法。 我建议刚通过不同的选择去,我始终把命令到一个单一变量,然后打印出变量,这样我可以看到我快要用完,如果你看到两条斜线,那么你可能知道有没有需要设法逃脱斜线,但是没有它打印到你不知道屏幕。

尝试经历的其他流程执行的功能,看看你的作品。 http://www.php.net/manual/en/ref.exec.php

我已经找到了一个工作最喜欢在命令行中运行它,是反引号操作符,然后接下来是system

尝试这样的事情。

$pyscript = 'C:\\wamp\\www\\testing\\scripts\\imageHandle.py';
$python = 'C:\\Python27\\python.exe';
$filePath = 'C:\\wamp\\www\\testing\\uploads\\thumbs\\10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd`


$pyscript = 'C:\wamp\www\testing\scripts\imageHandle.py';
$python = 'C:\Python27\python.exe';
$filePath = 'C:\wamp\www\testing\uploads\thumbs\10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd` 

编辑:春天来了! 我想通了,你周围的单蜱“”,这意味着里面的变量没有被替换您的命令。 使用双引号代替。 这将解决这个问题,但我还是建议呼应了命令运行它之前,所以,你会看到你正在运行的命令,你将能够识别这样的问题。



Answer 2:

在XAMPP SERVER我index.py文件像

from wordsegment import load, segment
load()
print(segment('morerecentlythedevelopmentwhichisapotent'))

我的index.php文件是这样

<html>
<head>
  <title>py script</title>
</head>

<body>
  <h1>Hey There!Python Working Successfully In A PHP Page.</h1>
  <?php
    $python = `python index.py`;
    echo $python;
    ?>
</body>
</html>

现在运行index.php文件。

  Hope this will help you 


文章来源: executing python script from php under windows