我试图显示/etc/postfix的内容/运输.in在
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 ""
print "
"print "
Data File %s" % fileNameprint ""
print "
"print "
- "
for line in lines:
print "
%s " % str(cgi.escape(line))print "
"print ""
print ""
除了我要显示的文件行之外,所有内容(包括标题在内的HTML标记)都按预期完成。Firebug将服务器的响应显示为:
^{pr2}$
但是,如果我从命令行运行脚本,我会得到:# sudo -u apache ./test3.py
Content-type:text/html
Data File /etc/postfix/transport.in- host.domain.com relay:[mailhub.domain.corpnet1]
- * relay:[host.mailrelay.com]
我确信这是件简单的事,但就我的一生而言,我无法弄明白…
我在RHEL7上运行python2.7.5/apache2.4.6。在