我使用Python中的子进程对象来调用PHP脚本。脚本运行得很好,我可以从进程中获取输出。
import subprocess
proc = subprocess.Popen("php /route/to/my/script/php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
但是,我需要向脚本发送一些参数。我怎样才能做到这一点。我想我需要通过Popen的一个参数发送帖子?
答案 0 :(得分:2)
我认为你应该在终端中使用args:
想象一下,您想要传递字符串参数arg1
,arg2
和arg3
。
你的python类应该看起来像
proc = subprocess.Popen(“php / route / to / my / script / php arg1 arg2 arg3”,shell = True, 标准输出= subprocess.PIPE)
然后参数的vardump看起来像
<?php var_dump($argv); ?>
并将返回:
array(4) {
[0]=>
string(10) "script.php"
[1]=>
string(4) "arg1"
[2]=>
string(4) "arg2"
[3]=>
string(4) "arg3"
}
那就是你如何添加参数