从windows下的php执行python脚本 [英] executing python script from php under windows

查看:303
本文介绍了从windows下的php执行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这与我的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上运行?哦,这也可以直接从Windows中的命令提示符下运行



编辑:



这是解决方案您可以在答案中阅读。

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

像一个魅力。

我发现奇怪的是, C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\ wamp\\ .... 将在Linux机器上工作。



这个问题可能涉及到几个事情,包括实现不同程序执行功能的操作系统特定的方法。我建议只是通过不同的选项,我总是把命令放入一个单一的变量,然后打印出变量,以便我可以看到我要运行,如果你看到两个斜线,那么你可能知道没有



尝试通过其他流程执行功能看看什么对你有用。
http://www.php.net/manual/en/ref .exec.php



我找到的工程MOST喜欢在命令行运行,是反引号操作符,然后是 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`

编辑:AHHH!我只是想出来,你是围绕你的命令与单个蜱'这意味着内部的变量不被替换。请使用双引号。这将解决问题,但我仍然建议在运行之前回显命令,以便您将看到您正在运行的命令,您将能够识别这样的问题。


Okay this works with my linux server but I'm testing my web site at home on my PC and am trying to get php to execute my python script and of course the \ are WINDOWS only the path changes when Im on my linux machine.

$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 );

this works on my linux machine but not my windows testing server. How do I get this to run on windows? Oh and this also works when running directly from the command prompt in windows

EDIT:

THIS IS THE SOLUTION as you can read in the answer.

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

works like a charm. Changed single ticks to double.

解决方案

I find it odd that the filePath of C:\\wamp\\.... would work on a Linux machine. Anyway, have you tried NOT escaping the slashes?

The issue can be related to several things, including OS specific methods of implementing the different program execution functions. I recommend just going through the different options, I always put the command into a single variable and then print out the variable so that I can see what I am about to run, if you saw two slashes, then you might know that there is no need to try to escape the slashes, however without printing it to the screen you wouldn't know.

Try going through the other process execution functions to see what works for you. http://www.php.net/manual/en/ref.exec.php

The one that I have found works MOST like running it at the command line, is the backtick operator, and then next is system

try something like this.

$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` 

EDIT: AHHH! I just figured it out, you are surrounding your command with single ticks '' which means the variable inside are not being replaced. Use double quotes instead. That WILL solve the problem, but I still recommend echoing the command out before you run it so that you would see the command you are running and you would be able to identify an issue like this.

这篇关于从windows下的php执行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆