在使用Python CGI时,是否有方法以不同于默认值的格式输出bash命令结果

在使用Python CGI时,是否有方法以不同于默认值的格式输出bash命令结果,python,python-3.x,bash,python-2.7,cgi,Python,Python 3.x,Bash,Python 2.7,Cgi,从Python向网页运行bash命令时,如果未设置输出选项,则会成功输出到网页。但是,我希望将输出保存到xml文件中,并将输出显示到网页中。我尝试执行此操作时出错: 代码: #/usr/bin/python 导入子流程 导入操作系统 #导入用于CGI处理的模块 导入cgi、cgitb;cgib.enable() 导入时间 #创建FieldStorage的实例 form=cgi.FieldStorage() #从字段中获取数据 niktoValue=form.getvalue('niktoInp

从Python向网页运行bash命令时,如果未设置输出选项,则会成功输出到网页。但是,我希望将输出保存到xml文件中,并将输出显示到网页中。我尝试执行此操作时出错:

代码:

#/usr/bin/python
导入子流程
导入操作系统
#导入用于CGI处理的模块
导入cgi、cgitb;cgib.enable()
导入时间
#创建FieldStorage的实例
form=cgi.FieldStorage()
#从字段中获取数据
niktoValue=form.getvalue('niktoInput')
#从字段中获取数据
打印“内容类型:text/html\r\n\r\n”
打印“”
打印“”
打印“”
打印“”
打印“Nikto扫描%s的信息”%(niktoValue)
def bash(命令):
返回子进程。检查_输出(['bash','-c',command])
def niktoScan():
res=bash(“nikto-h%s-output xml”%niktoValue.splitlines())
打印(res)
日航
打印“”
打印“”
错误13通常为“权限被拒绝”

我怀疑您的CGI脚本是以
\u www
用户的身份运行的,而不是在桌面上登录时以普通用户的身份运行的。您可以通过在http服务器下的
bash
脚本中运行
id
,或者查看http服务器配置文件来进行测试,可能是
/etc/apache2/httpd.conf
中的以下部分:

用户/组:要作为运行httpd的用户/组的名称(或#编号)。 通常,为用户创建一个专用的用户和组是一个很好的做法 运行httpd,与大多数系统服务一样

用户(www 集团www

我不熟悉
nikto
程序,因此您需要从其作者那里了解如何允许
www
用户运行它-可能通过将该用户添加到Unix组,可能通过
sudo
,或者其他方式

#!/usr/bin/python
import subprocess
import os
# Import modules for CGI handling
import cgi, cgitb; cgitb.enable()
import time

# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
niktoValue = form.getvalue('niktoInput')


# Get data from fields
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "</head>"
print "<body>"
print "<h2>Nikto Scan Information for %s </h2>" % (niktoValue)


def bash(command):
    return subprocess.check_output(['bash', '-c', command])

def niktoScan():
    res = bash("nikto -h %s -output xml" % niktoValue).splitlines()
    print(res)



niktoScan()

print "</body>"
print "</html>"