PHP 和 Python 之间发送数据时遇到问题

发布于 2024-10-07 14:45:54 字数 836 浏览 0 评论 0原文

我一直在尝试使用 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/sea​​rch.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2024-10-14 14:45:54

有两件事。您确定脚本正在完成吗?它可能挂在 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?

梦晓ヶ微光ヅ倾城 2024-10-14 14:45:54

您确定脚本会运行吗?

检查 $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.

流殇 2024-10-14 14:45:54

我想出了主意并决定 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文