从Python 1:1调用PHP [英] 1:1 call PHP from Python
问题描述
我们正在使用Splunk(一种用于分析机器数据(如日志文件)的工具),并在PHP中有一个应用程序.对于某些数据,我们需要使用php(基于CLI)调用我们的应用程序.不幸的是,Splunk仅支持Python调用.
有没有一种简单的方法可以使用相同的参数将1:1的转发/调用" php并返回输出,例如"passthru".我仅使用所谓的子流程模块找到了解决方案的一部分,但我的python经验为零,所以无法正常工作.
We're using Splunk (A tool to analyse machine data like log files) and have an application in PHP. For some data we need to do a call to our application in php (CLI-based). Unfortunately Splunk only supports Python calls.
Is there an easy way to 1:1 "forward/call" php with the same arguments and return the output, like a "passthru". I've found only parts of the solution with the socalled subprocess module but my python experience is zero, so can't get it to work.
例如,splunk调用: python external_lookup.py参数1参数2参数3
-然后应调用python脚本(具有给python的CLI参数): php external_lookup.php参数1参数2参数3
-然后php写入其输出
-Python捕获该输出并自行输出
For example, splunk calls:
python external_lookup.py argument1 argument2 argument3
- Then the python script should call (with the CLI arguments given to python):
php external_lookup.php argument1 argument2 argument3
- Then php writes its output
- Python captures that output and outputs it itself
任何帮助都值得赞赏,或者一个更好的示例脚本.
Any help much appreciated, or a working example script even better.
在此先感谢,
文斯
Thanks in advance,
Vince
推荐答案
使用 subprocess.call
:
import sys, subprocess
sys.exit(subprocess.call(['php', sys.argv[0].replace('.py', '.php')] + sys.argv[1:]))
使Python脚本返回PHP脚本返回的值.
Made Python script return the value the PHP script returned.
这篇关于从Python 1:1调用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!