executing python script from php under windows

2019-04-06 23:03发布

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.

2条回答
一纸荒年 Trace。
2楼-- · 2019-04-06 23:28

In XAMPP SERVER my index.py file be like

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

my index.php file be like

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

Now run the index.php file.

    Hope this will help you
查看更多
成全新的幸福
3楼-- · 2019-04-06 23:32

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.

查看更多
登录 后发表回答