这里是新手。任何帮助都将不胜感激。。在
我正在编写一个cgi脚本来运行tcp-pcap诊断工具。如果我在bash中运行该命令,它将如下所示:
/home/fsoft/cap/capnostic -r 38350 /home/fsoft/brad.pcap > 38350
所以我试着用python来做:
^{pr2}$我觉得“>;”把事情搞砸了。。但我似乎找不到正确的语法。。同样,一旦我得到正确的命令,我就可以打印输出变量?在
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"
在
'>'
之前缺少一个+
请注意,
^{pr2}$os.system
不返回命令的输出,下面是如何获得该结果的方法:缺少用于连接字符串和字符串之间空格的
+
。您可以使用string formatting简化任务,或者只需在需要的地方添加+
和空格:注意:%s用于将每个变量视为字符串。
使用os.system替换为subprocess模块:
^{pr2}$要捕获输出,您需要使用^{} ,其翻译如下:
相关问题 更多 >
编程相关推荐