开发者

Trouble sending data between PHP and Python

开发者 https://www.devze.com 2023-01-30 16:01 出处:网络
I\'ve been trying to set up a very basic search engine using the Whoosh modules in python called on from PHP. I had it working until I upgraded the modules for some additional features I needed. At wh

I've been trying to set up a very basic search engine using the Whoosh modules in python called on from PHP. I had it working until I upgraded the modules for some additional features I needed. At which point an odd bug seemed to appear. Any print statement after the line "searcher = ixtemp.searcher()" is not being received by the PHP script.

The python search script is called from PHP and the first result is displayed with the following commands

exec("python print.py",$output,$ret_code);

echo $output[0];

The python script -

from whoosh.index import open_dir

ixtemp = open_dir("index")

searcher = ixtemp.searcher()

results = searcher.find("content", u""+"tes开发者_StackOverflow中文版t")

for k in results:
    print k['filename']

Running the PHP now gives the following error -

Notice: Undefined offset: 0 in /opt/lampp/htdocs/new/search.php on line 17

The python script is working when I run it by itself. After a little investigation it seems that any print statements before the line "searcher = ixtemp.searcher()" can be read by the PHP, but all after are not received by the PHP script. I've also tried the popen() and proc_open functions too, but they have the same problem.

Any ideas on what the problem is or how I can work around it?

Thanks


Two things. Are you sure the script is completing? It could be hanging on the searcher() call. Second, I suppose searcher() could be redirecting stdout.

My guess is that your script isn't completing, or it's timing out or something.

EDIT Looks like this code depends on the current working directory. You mention below that it runs fine in the interpreter - in the same wd as the webserver sets up?


Are you sure the script runs at all?

Check the $return_code.

Its possible it cannot locate your print.py script or perhaps even the python.exe.


I ran out ideas and decided to chmod 777 the directory...and...it worked. I'm not sure why it did since data could always be sent above the searcher() function anyway.

Perhaps while initiating the search the method was trying to modify protected index files?

But that couldn't in itself be the problem since it always worked from the interpreter.

So it must have been the PHP file. It seems to have been protection against indirectly modifying the index files.

0

精彩评论

暂无评论...
验证码 换一张
取 消