【发布时间】:2013-07-18 11:47:07
【问题描述】:
我在 Windows Server 2008 上使用以下 python 脚本:
import cgitb
import subprocess
cgitb.enable()
print "Content-Type: text/plain;charset=utf-8"
print
cmd = "git tag"
process = subprocess.Popen(cmd.split(), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
git_output = process.communicate()[0]
print "git output = %s" % git_output
事实上,有一些 git 标签。通过 shell 运行此脚本可以完美运行。但是,通过 IIS (7) 运行时,输出似乎为空。
我尝试将 Popen 输出定向到文件而不是 PIPE。同样,从命令行运行时工作,通过 IIS 运行时不起作用。
有什么想法吗?
编辑:
按照@Wooble 的建议,我从调用通信中删除了 [0] 以查看 git 错误,并且确实发现了神秘错误“'git' is not known as an internal or external command, operable program or batch file。 "当然 git 是安装在系统上的,正如我所说的,脚本在直接通过命令行运行时可以工作。
没有用,我试过了:
- 将命令设置为使用 git 可执行文件的完整路径
- 将git可执行目录的完整路径添加到python的sys.path中
- 将实际的 git 可执行文件复制到工作目录 - 这消除了“git not Recognized”错误,但仍产生空结果!
请帮忙!!
【问题讨论】:
-
是否至少在页面开头打印“git output =”?您确定脚本的工作目录是您认为的那样吗?您的错误日志中有什么内容吗?
-
是的,它打印“git output =”。我确定工作目录,因为当我将输出定向到文件时,该文件实际上是在预期的目录中打开的,但是是空的(从 shell 运行时,它包含预期的输出)。日志中没有任何内容,除非我没有找到正确的位置 - 我查看了 C:\inetpub\logs(我对 IIS 很陌生)。
-
哦,对,您正在捕获 stderr,因此它不会进入您的日志。尝试去掉最后一行旁边的
[0],这样你就可以看到 git 的错误输出(如果有的话)。 -
天哪,“'git' 不被识别为内部或外部命令、可运行程序或批处理文件。”但是......它可以从命令行运行,为什么会发生这种情况?我必须将 git 添加到某些 IIS 路径变量吗?
标签: python iis cgi subprocess