为什么此脚本不会将输出打印到网页?
它给我的:使用Flask将脚本输出打印到网页
*运行于http://127.0.0.1:5000/(按Ctrl + C退出)
消息,但不执行打印的网页。
from flask import Flask
app = Flask(__name__)
import wmi
ip=['server1','server2','server3','server4','server5']
user="username"
password="password"
append_services=[]
words = 'win32'
@app.route("/")
def service_status():
for a in ip:
global append_services
print ('\n'+a+'\n')
c = wmi.WMI (a,user=user,password=password)
get_names= c.Win32_Service()
for y in get_names:
convert = str(y.Name)
append_services.append(convert)
append_services=[w for w in append_services if w.startswith(words)]
for l in append_services:
state_of_services = c.Win32_Service(Name=l)
if state_of_services:
for x in state_of_services:
convert1 = str(x.State)
convert2 = str(x.Caption)
print convert1," ",convert2
if __name__ == "__main__":
app.run()
如果使用'print',它将在控制台上打印。如果你想在html上打印它供用户看,你可以尝试返回它(而不是打印,我从来没有使用这个设置),或者将它传递给你的渲染模板,然后把它放到html中。 –
返回在('\ n'+ a +'\ n')打印服务器名称但返回不起作用于'convert1,','covert2' –
因为当你返回时,函数已经停止I认为。你需要将它们都保存在一个变量中,当你退出循环时,返回它们。 –