import functools
import random
import time
from datetime import datetime
import psutil
from flask import Flask,request,render_template,redirect,session
import os
app = Flask(__name__)
user_info = [
{'name':'root',
'passwd':'westos'}
]
app.config['SECRET_KEY'] = random._urandom(24)
@app.route('/index/')
def index():
return render_template('index.html')
@app.route('/login/',methods=['POST','GET'])
def login():
if request.method == 'POST':
session['user'] = request.form['name']
session['passwd'] = request.form['passwd']
user = request.form['name']
passwd = request.form['passwd']
for i in user_info:
if i['name'] == 'root' and i['passwd'] == 'westos':
return redirect('/index/')
else:
return render_template('login.html',message='用户不存在或者密码错误')
else:
return render_template('login.html')
@app.route('/register/',methods=['POST','GET'])
def register():
if request.method == 'POST':
user = request.form['name']
passwd = request.form['passwd']
for i in user_info:
if i['name'] == user:
return render_template('register.html', message='用户已经存在,请重新输入')
else:
user_info.append(dict(name=user,passwd=passwd))
return redirect('/login/')
else:
return render_template('register.html')
@app.route('/logout/')
def logout():
session.pop('user')
session.pop('passwd')
return redirect('/login/')
def is_login(f):
functools.wraps(f)
def wrapper(*args,**kwargs):
if 'user' in session:
f(*args,**kwargs)
else:
return redirect('/login/')
return wrapper
# 系统监控
@app.route('/system/')
@is_login
def system():
info = os.uname()
nodename = info.nodename,
sysname = info.sysname,
machine = info.machine,
now_time = datetime.now(),4
boot_time = datetime.fromtimestamp(psutil.boot_time())
delta_time = datetime.now()-datetime.fromtimestamp(psutil.boot_time())
return render_template('system.html',
nodename=nodename,
sysname=sysname,
machine=machine,
now_time=now_time,
boot_time=boot_time,
delta_time=delta_time
)
app.run(port=5002)
对数据库进行连接