1:1 call PHP from Python

前端 未结 2 911
悲&欢浪女
悲&欢浪女 2021-01-28 06:26

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). Unf

相关标签:
2条回答
  • 2021-01-28 06:35

    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.

    0 讨论(0)
  • 2021-01-28 06:52

    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.

    0 讨论(0)
提交回复
热议问题