PHP 和 Python 之间发送数据时遇到问题
我一直在尝试使用 PHP 调用的 python 中的 Whoosh 模块来建立一个非常基本的搜索引擎。我一直让它工作,直到我升级模块以获得我需要的一些附加功能。这时似乎出现了一个奇怪的错误。 PHP 脚本不会接收“searcher = ixtemp.searcher()”行之后的任何打印语句。
从 PHP 调用 python 搜索脚本,并使用以下命令显示第一个结果
exec("python print.py",$output,$ret_code);
echo $output[0];
python 脚本 -
from whoosh.index import open_dir
ixtemp = open_dir("index")
searcher = ixtemp.searcher()
results = searcher.find("content", u""+"test")
for k in results:
print k['filename']
运行 PHP 现在会出现以下错误 -
注意:未定义的偏移量:0 in /opt/lampp/htdocs/new/search.php 上 第 17 行
当我单独运行 python 脚本时,它正在工作。经过一番调查后,PHP 似乎可以读取“searcher = ixtemp.searcher()”行之前的任何打印语句,但 PHP 脚本不会接收其后的所有打印语句。我也尝试过 popen() 和 proc_open 函数,但它们也有同样的问题。
关于问题是什么或如何解决它有什么想法吗?
谢谢
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""+"test")
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两件事。您确定脚本正在完成吗?它可能挂在 searcher() 调用上。其次,我认为 searcher() 可能会重定向标准输出。
我的猜测是您的脚本尚未完成,或者超时或其他原因。
编辑 看起来此代码取决于当前工作目录。您在下面提到它在解释器中运行良好 - 与网络服务器设置的 wd 相同?
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?
您确定脚本会运行吗?
检查 $return_code。
它可能无法找到您的 print.py 脚本,甚至可能找不到 python.exe。
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.
我想出了主意并决定 chmod 777 该目录......并且......它起作用了。我不确定为什么会这样做,因为无论如何数据总是可以在 searcher() 函数之上发送。
也许在启动搜索时该方法试图修改受保护的索引文件?
但这本身并不是问题,因为它总是通过口译员起作用。
所以它一定是 PHP 文件。它似乎可以防止间接修改索引文件。
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.