为什么此脚本不将输出打印到网页? 它给了我:
*在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()
答案 0 :(得分:0)
您需要在变量中获取所有结果,生成您的html然后返回,以便用户可以看到它。让我们试试:
from flask import Flask
import wmi
app = Flask(__name__)
ip = ['server1', 'server2', 'server3', 'server4', 'server5']
user = "username"
password = "password"
append_services = []
words = 'win32'
@app.route("/")
def service_status():
results = [] # Temp for result to return to html
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)
results.append([a, [convert1, convert2]]) # Append results
print(convert1 + " " + convert2)
# This part for generate HTML for return
html = ''
for i in results:
html += '<h2>Ip: ' + i[0] + '</br>'
html += '<h3>From ' + i[1][0] + ' to ' + i[1][1]
return html
if __name__ == "__main__":
app.run()