【问题标题】:Can't get output from Popen when run through IIS通过 IIS 运行时无法从 Popen 获取输出
【发布时间】: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 是安装在系统上的,正如我所说的,脚本在直接通过命令行运行时可以工作。

没有用,我试过了:

  1. 将命令设置为使用 git 可执行文件的完整路径
  2. 将git可执行目录的完整路径添加到python的sys.path中
  3. 将实际的 git 可执行文件复制到工作目录 - 这消除了“git not Recognized”错误,但仍产生空结果!

请帮忙!!

【问题讨论】:

  • 是否至少在页面开头打印“git output =”?您确定脚本的工作目录是您认为的那样吗?您的错误日志中有什么内容吗?
  • 是的,它打印“git output =”。我确定工作目录,因为当我将输出定向到文件时,该文件实际上是在预期的目录中打开的,但是是空的(从 shell 运行时,它包含预期的输出)。日志中没有任何内容,除非我没有找到正确的位置 - 我查看了 C:\inetpub\logs(我对 IIS 很陌生)。
  • 哦,对,您正在捕获 stderr,因此它不会进入您的日志。尝试去掉最后一行旁边的[0],这样你就可以看到 git 的错误输出(如果有的话)。
  • 天哪,“'git' 不被识别为内部或外部命令、可运行程序或批处理文件。”但是......它可以从命令行运行,为什么会发生这种情况?我必须将 git 添加到某些 IIS 路径变量吗?

标签: python iis cgi subprocess


【解决方案1】:

我不知道为什么从 IIS 运行时路径是部分的(欢迎解释!),但添加这个最终解决了问题:

git_path = "C:\\Program Files (x86)\\Git\\bin\\"
os.environ["PATH"} += git_path + ';'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 2020-08-18
    • 2017-05-16
    • 2015-10-09
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多