f = open()/在命令行中工作但不作为CGI脚本[python]

时间:2011-04-12 14:44:29

标签: python cgi

    #! /usr/bin/env python

import htmlSplitter, htmlGlue

headerContent, mainContent, sideSetup, sideContent, footerContent = htmlSplitter.split("../htdocs/bcc/register.html")


mainContent = "<h1>This is another Test</h1>"
sideContent = "<h2>Jonathan's here!</h2>"


htmlDoc = htmlGlue.glue(headerContent, mainContent, sideSetup, sideContent, footerContent)

f = open("../bcc/doctest.html", "w")
f.write(htmlDoc)
f.close()

print("Location:../bcc/doctest.html")
print

当我从命令行运行它时,此脚本运行正常。但是,当我将其作为CGI脚本运行时,它会给我一个“过早结束脚本标题”错误。我已经调试了,如果我注释掉打开并写入“doctest.html”的段,那就没问题了(所以我知道我包含的其他两个模块没有引起问题)。知道为什么这部分代码不能用作CGI脚本吗?有什么我应该代替吗?

注意:我在'doctest.html'上做了一个chmod a + rw,以确保该脚本具有编辑权限。

谢谢!

2 个答案:

答案 0 :(得分:0)

也许只是一个复制/粘贴错误,但你的shebang似乎是缩进的。 unindent它,并尝试通过服务器上的命令行启动脚本,使用./yourscript(不是python yourscript,因为这可能是网络服务器执行的方式)

答案 1 :(得分:0)

将以下内容添加到脚本的开头:

#!/usr/bin/env python
print 'Content-Type: text/html'
print

我总是在我的python cgi脚本中声明Content-Type,这很有礼貌。

import cgi
import cgitb
cgitb.enable() # allows error tracebacks
如果您的脚本出现问题,

cgitb会向您显示一个很好的基于Web的回溯,例如抛出任何异常。

相关问题