从PHP脚本调用Python脚本时,不会通过PHP调用创建在控制台运行时创建的临时文件

时间:2022-08-12 01:14:49

Scenario:

I have a php page in which I call a python script.

我有一个php页面,我在其中调用python脚本。

Python script when run on the command line (Linux) shows output on the command line, as well as writes the output to a file.

在命令行(Linux)上运行时,Python脚本在命令行上显示输出,并将输出写入文件。

Python script when run through php, doesn't do either.

通过php运行时的Python脚本也没有。

Elaboration:

I use a simple system command in PHP to run the python script as:

我在PHP中使用一个简单的系统命令来运行python脚本:

/var/www/html/1.php: system('/usr/python/bin/python3 ../cgi-bin/tabular.py 1');

/var/www/html/1.php:system('/ usr / python / bin / python3 ../cgi-bin/tabular.py 1');

/var/www/cgi-bin/tabular.py --This python file basically parses a data file, uses python's regular expression to search for specific headings and outputs the headings to the stdout, as well as write it to a file.

/var/www/cgi-bin/tabular.py - 这个python文件基本上解析数据文件,使用python的正则表达式搜索特定标题并将标题输出到标准输出,并将其写入文件。

This python script has a few routines in it which get executed, so I put print statements to debug. I noticed only a few initial print statements' output in the PHP page, all the ones from the function that actually does something are not seen.

这个python脚本中有一些例程可以执行,因此我将pr​​int语句调试。我注意到PHP页面中只有一些初始打印语句的输出,所有来自实际执行某些操作的函数都没有看到。

Also, as part of my test, I thought well the py script is in a different folder so let me change it to the /var/www/html folder, no go.

另外,作为我测试的一部分,我认为py脚本位于不同的文件夹中,所以让我将其更改为/ var / www / html文件夹,不用了。

I hope I captured the problem statement with sufficient detail and someone is able to reproduce this issue at their end. If I make any progress on this one myself, I'll annotate this question. Thanks everyone.

我希望我能够充分详细地捕获问题陈述,并且有人能够在最后重现这个问题。如果我自己在这个上取得任何进展,我会诠释这个问题。感谢大家。

Gaurav

3 个解决方案

#1


I bet your py script has some bug which couses it to break when called from inside PHP. Try

我打赌你的py脚本有一些错误,当从PHP内部调用时它会破坏它。尝试

passthru('/usr/python/bin/python3 ../cgi-bin/tabular.py 1 2>&1');

to investigate (notice 2>&1 which causess stderr to be written to stdout).

调查(通知2>&1导致stderr被写入stdout)。

#2


A permission problem is most likely the case.

许可问题很可能就是这种情况。

If apache is running as apache, then it will not have access to write to a file unless

如果apache作为apache运行,那么它将无权写入文件,除非

  1. The file is owned by apache
  2. 该文件归apache所有

  3. The file is in the group apache and group writable
  4. 该文件位于组apache和组可写

  5. The file is world writable
  6. 该文件是世界可写的

This is a "sticky" problem on a multi-user machine, as different people have access to Apache.

这是多用户计算机上的“粘性”问题,因为不同的人可以访问Apache。

Try chmod 666 output.txt on the file and then re-run your test.

在文件上尝试chmod 666 output.txt,然后重新运行测试。


Considerations:

  1. Have the python script write the output to a database
  2. 让python脚本将输出写入数据库

  3. Use PHP's popen functionality to open the process and communicate over pipes
  4. 使用PHP的popen功能打开进程并通过管道进行通信

  5. Re-write using PHP's regular expressions
  6. 使用PHP的正则表达式重写

  7. Write the output file to /tmp and then read the results using PHP as soon as the python script is done.
  8. 将输出文件写入/ tmp,然后在python脚本完成后立即使用PHP读取结果。

  9. etc...

#3


Check that the user the python script is running is has write permissions in CWD. Also, try shell_exec() or passthru() to call the script, rather than system().

检查python脚本正在运行的用户是否具有CWD的写权限。另外,尝试使用shell_exec()或passthru()来调用脚本,而不是system()。

#1


I bet your py script has some bug which couses it to break when called from inside PHP. Try

我打赌你的py脚本有一些错误,当从PHP内部调用时它会破坏它。尝试

passthru('/usr/python/bin/python3 ../cgi-bin/tabular.py 1 2>&1');

to investigate (notice 2>&1 which causess stderr to be written to stdout).

调查(通知2>&1导致stderr被写入stdout)。

#2


A permission problem is most likely the case.

许可问题很可能就是这种情况。

If apache is running as apache, then it will not have access to write to a file unless

如果apache作为apache运行,那么它将无权写入文件,除非

  1. The file is owned by apache
  2. 该文件归apache所有

  3. The file is in the group apache and group writable
  4. 该文件位于组apache和组可写

  5. The file is world writable
  6. 该文件是世界可写的

This is a "sticky" problem on a multi-user machine, as different people have access to Apache.

这是多用户计算机上的“粘性”问题,因为不同的人可以访问Apache。

Try chmod 666 output.txt on the file and then re-run your test.

在文件上尝试chmod 666 output.txt,然后重新运行测试。


Considerations:

  1. Have the python script write the output to a database
  2. 让python脚本将输出写入数据库

  3. Use PHP's popen functionality to open the process and communicate over pipes
  4. 使用PHP的popen功能打开进程并通过管道进行通信

  5. Re-write using PHP's regular expressions
  6. 使用PHP的正则表达式重写

  7. Write the output file to /tmp and then read the results using PHP as soon as the python script is done.
  8. 将输出文件写入/ tmp,然后在python脚本完成后立即使用PHP读取结果。

  9. etc...

#3


Check that the user the python script is running is has write permissions in CWD. Also, try shell_exec() or passthru() to call the script, rather than system().

检查python脚本正在运行的用户是否具有CWD的写权限。另外,尝试使用shell_exec()或passthru()来调用脚本,而不是system()。