问题
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.
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
回答1:
Using subprocess.call
:
import sys, subprocess
sys.exit(subprocess.call(['php', sys.argv[0].replace('.py', '.php')] + sys.argv[1:]))
Edit: Made Python script return the value the PHP script returned.
回答2:
Using Popen
from the subprocess module:
import sys
from subprocess import Popen
output = subprocess.Popen(['php', 'path/to/script.php'] + sys.argv[1:], stdout=subprocess.PIPE).communicate()[0]
sys.argv[1:]
contains every command line argument except the name of python script itself.
来源:https://stackoverflow.com/questions/12536190/11-call-php-from-python