Reputation: 57
Why is this script not printing the output to a webpage?
It gives me the:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
message but nothing prints to the webpage.
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()
Upvotes: 0
Views: 8442
Reputation: 896
You need to get all the results in a variable, generate your html then return so user can see it. let's try:
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()
Upvotes: 0