【发布时间】:2012-08-10 18:48:01
【问题描述】:
这里是新手。任何帮助将不胜感激..
我正在编写一个运行 tcp pcap 诊断工具的 cgi 脚本。如果我在 bash 中运行命令,它看起来像:
/home/fsoft/cap/capnostic -r 38350 /home/fsoft/brad.pcap > 38350
所以我想在 python 中做到这一点:
output = os.system('/home/fsoft/cap/capnostic -r' + port + directory+filename '>' + jobdir+filename
我感觉 '>' 搞砸了。但我似乎找不到正确的语法。而且,一旦我正确获得命令,我就可以打印输出变量了吗?
print '%s' % (output)
输出可能是3页数据..
感谢您的帮助。
这是我的完整代码:
#!/usr/bin/env python
import cgi, os
import cgitb; cgitb.enable()
import subprocess
form = cgi.FieldStorage()
port = form.getvalue("port")
filename = form.getvalue("filename")
directory = form.getvalue("directory")
jobdir = '/var/www/jobs/' + filename
def createdir():
os.system('mkdir /var/www/jobs/' + filename)
createdir()
def capout():
output = os.system('/home/fsoft/cap/capnostic -r %s %s%s > %s%s' % (port, directory, filename, jobdir, filename))
capout()
def htmlout():
print 'Content-type: text/html\n'
print '<html>'
print '<head>'
print '<title>Capnostic Output</title>'
print '</head>'
print '<body>'
print '<BR><BR><BR><center>'
print '<table border=0>'
print '<TR>'
print '<TD><center>port = %s<BR>filename = %s<BR>Directory = %s<BR>Job Directory = %s</TD>' % (port,filename,directory,jobdir)
print '</TR>'
print '</table>'
print '<BR><BR><BR>'
print '%s' % (output)
print '</body>'
print '</html>'
htmlout()
它现在告诉我:
<type 'exceptions.NameError'>: global name 'output' is not defined
args = ("global name 'output' is not defined",)
message = "global name 'output' is not defined"
【问题讨论】:
-
print '%s' % (output)可能是多余的。如果你能做到这一点,你应该能够做到print output。 -
根据official docs
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using os function