问题描述
我试图通过外壳程序运行另一个脚本,该脚本使用一组修改后的环境变量.
I'm trying to trying to run another script via the shell, that uses a modified set of environment variables.
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
运行上面的代码后,出现以下错误:
After running the code above, I get the following error:
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
传递环境变量时出现错误...我也尝试过将它们作为字符串传递-错误并指出需要一个映射对象.但是,实际上,正在传递的环境是一个映射对象...
The error comes when passing the environment variables... I've also tried passing them as a string - It errors out and says that it needs a mapping object. However, as it is, the environ being passed IS a mapping object...
出什么问题了?
其他信息:我在Ubuntu 12.04.1上运行Python 2.7
Additional Information:I am running Python 2.7 on Ubuntu 12.04.1
推荐答案
从注释中复制答案,以便从未答复"过滤器中删除此问题:
Copying the answer from the comments in order to remove this question from the "Unanswered" filter:
"...... Python 2.x中的键以及可能的值也必须是字节字符串.因此,如果您使用的是unicode字符串,请确保将其编码为 utf-8
.另外,如果默认情况下通过__future__ import unicode_literals 中的使用Unicode文字,请确保字典键的字符串文字以
b
为前缀,而不是字节文字unicode文字."
"...the keys, and possibly the values also, in Python 2.x need to be byte strings. So if you are using unicode strings, make sure you encode them to utf-8
. Also, if you are using unicode literals by default via from __future__ import unicode_literals
make sure your string literals for the dictionary keys are prefixed with b
to be byte literals instead of unicode literals."
〜根据 Pedro Romano
这篇关于subprocess.Popen execve()arg 3包含非字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!