我試圖通過shell來運行另一個腳本,該腳本使用了一組經過修改的環境變量。subprocess.Popen execve()arg 3包含非字符串值
def cgi_call(script, environ):
pSCRIPT = subprocess.Popen(script, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE, env=environ, shell=True)
pc = pSCRIPT.communicate()
status = "200 OK"
headers = [('Content-Type',"text/html")]
if pc[1] != '':
raise RuntimeError, pc[1]
else:
rval = str(pc[0])
return status, headers, rval
運行上面的代碼後,我得到以下錯誤:
File "server/httpd.py", line 76, in DynamicServer
status, headers, rval = handler(environ)
File "server/httpd.py", line 43, in handler
status, headers, rval = cgi_call(srvpath+"../www/public_html"+environ["PATH_INFO"]+'index.py',environ)
File "server/httpd.py", line 21, in cgi_call
stdin=subprocess.PIPE, env=environ, shell=True)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
<type 'exceptions.TypeError'> execve() arg 3 contains a non-string value
通過環境變量...我也試着將它們作爲一個字符串時,錯誤出現 - 它的錯誤並表示它需要一個映射對象。然而,實際情況是,通過的環境是地圖對象...
問題是什麼?
附加信息: 我在Ubuntu 12.04.1
'execve()arg 3'是環境。你確定你傳遞的'env'參數只包含字符串嗎? –
是的,我很積極。這是我檢查的第一件事。 –
我不確定,但我相信至少在Python 2.x中的鍵和值也可以是字節字符串。所以如果你使用unicode字符串,確保你將它們編碼爲'utf-8'。另外,如果默認使用unicode字面值,通過'from __future__ import unicode_literals'確保字典鍵的字符串字面值以'b'爲前綴,而不是字節文字而不是Unicode字面值。 –