2013-11-02 41 views
1

我使用Python中的子流程對象調用PHP腳本。腳本運行得很好,我可以從流程中獲得輸出。將參數添加到Python中調用的PHP腳本中

import subprocess 

proc = subprocess.Popen("php /route/to/my/script/php", shell=True, stdout=subprocess.PIPE) 
script_response = proc.stdout.read() 

但是,我需要發送一些參數給腳本。我怎樣才能做到這一點。我會假設我需要通過Popen的參數之一發送帖子?

回答

2

我想你應該在終端使用args來:

想象一下,你想傳遞字符串參數arg1arg2arg3

你的Python類,那麼應該看起來像

proc等於subprocess.Popen( 「PHP /路由/到/我/腳本/ PHP ARG1參數3」,殼=真, 標準輸出= 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" 
} 

那麼這就是你怎麼可以添加parameers

+1

謝謝!工作得很好! – Dynelight