【发布时间】:2016-06-21 01:13:05
【问题描述】:
我正在尝试通过 python 和 cgi 在<div> 中显示 /etc/postfix/transport.in 的内容。当我从命令行运行脚本时,它会按预期工作,但是,当从网页调用时,它不会显示文件内容。这就是我的 CGI 中的内容:
#!/usr/bin/env python
import cgi, os.path
# fileName = "/etc/postfix/transport.in"
form = cgi.FieldStorage()
fileName = form['file'].value
def safePlainText(s):
newString = s.replace('&', '&').replace('<', '<')
return newString
def fileLinesToHTMLLines(fileName):
safeLines = list()
if os.path.exists(fileName): # test if the file exists yet
lines = fileToStr(fileName).splitlines()
for line in lines:
safeLines.append(safePlainText(line))
return safeLines
def fileToStr(fileName):
fin = open(fileName);
contents = fin.read();
fin.close()
return contents
lines = fileLinesToHTMLLines(fileName)
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Data File %s</title>" % fileName
print "</head>"
print "<body>"
print "<ul>"
for line in lines:
print "<li> %s </li>" % str(cgi.escape(line))
print "</ul>"
print "</body>"
print "</html>"
除了我试图显示的文件行之外,所有内容(包括标题的 HTML 标记)都按预期出现。 Firebug 显示来自服务器的响应为:
<html>
<head>
<title>Data File transport.in</title>
</head>
<body>
<ul>
</ul>
</body>
</html>
但是,如果我从命令行运行脚本,我会得到:
# sudo -u apache ./test3.py
Content-type:text/html
<html>
<head>
<title>Data File /etc/postfix/transport.in</title>
</head>
<body>
<ul>
<li> host.domain.com relay:[mailhub.domain.corpnet1] </li>
<li> * relay:[host.mailrelay.com] </li>
</ul>
</body>
</html>
我确信这很简单,但对于我的生活,我无法弄清楚......
我在 RHEL7 上运行 Python 2.7.5 / Apache 2.4.6。
【问题讨论】: